2

Possible Duplicate:
C++ - What should go into an .h file?

I am a complete noob with C++ and slowly learning. I come from a C# background. I understand what can go into the header file versus what can go into the actual cpp implemention file. But what is the best practice for what goes where? For example, you can declare the class in the header and forward declare functions, but you can also have implementation details as inline functions.

Community
  • 1
  • 1
Kenoyer130
  • 6,874
  • 9
  • 51
  • 73

4 Answers4

3

Headers are usually reserved for your class/struct definitions, macro's and inline functions. In your CPP files you tend to have the longer implementations of your methods and member methods for your classes. Usually you only want to forward declare functions in your header that you want to allow others to use (if they do not already have an access modifier because of a class).

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
  • 1
    Would the header also include private variables or would those be better in the CPP ? – Kenoyer130 Jul 22 '11 at 03:07
  • Global members are probably better placed in the CPP unless you want others that include your header to be able to use those variables as well. For your case private should be in the CPP (or encapsulated in a class if need be) – Jesus Ramos Jul 22 '11 at 03:07
  • Global members are the work of Satan. So would the class private variables be declared in the header class definition? – Kenoyer130 Jul 22 '11 at 03:10
  • Yes your full class definitions should be in the header (without implementation obviously) – Jesus Ramos Jul 22 '11 at 03:12
  • Ok that part is mildly confusing coming from c#. Why would the "public" definition also contain private information! I think I got it now, thanks for the help. – Kenoyer130 Jul 22 '11 at 03:14
  • They can only see variable names they can't actually do anything with them because of the access modifier and only your internal code in the CPP file should be changing that. Same in C# as you can see that a class has private members but you cant change them unless you change the class' code. – Jesus Ramos Jul 22 '11 at 03:17
  • 1
    @MGZero, thanks for the correction on terminology its the declaration of your class that goes in there. Definition means implementation in the cpp file. – Jesus Ramos Jul 22 '11 at 03:18
  • @Kenoyer130, the private variables must be declared in the .h file so that the compiler knows the full definition when it needs to create an instance of the class. It's an unfortunate artifact of the way compilers operate - they want to know the detailed layout so they can reserve the proper amount of memory. – Mark Ransom Jul 22 '11 at 03:46
  • 1
    `class X;` is a declaration, `class X { ... };` is a definition. – fredoverflow Jul 22 '11 at 06:33
2

In general, declarations belong in the header and definitions belong in the cpp. There are some exceptions, two that I've run into are inline functions and templates. These both need to be defined in the header file with the function prototype.

MGZero
  • 5,812
  • 5
  • 29
  • 46
  • 2
    Actually inline definitions can be done in the .cpp, as long as every .cpp has identical definitions. For example, if it were a private function that was only used in the .cpp for the class you would only need to define it there. – Mark Ransom Jul 22 '11 at 03:49
  • Interesting, didn't know that. – MGZero Jul 22 '11 at 13:05
1

There are basic two considerations here: the needs of the compiler and the goal of good source code organization. In general, I find that header files should contain as little code as possible while still keeping the compiler happy. Header files should also #include as few other header files as possible, preferring forward type declarations to #include statements whenever possible. This reduces the number of dependencies between your source files and reduces compile time. It also helps avoid circular dependencies among source files.

While almost all function implementations go in the .cpp file, template functions and inline functions need to be available to the compiler when compiling dependent types. Rather than putting these in the header file, I prefer to put them in a third file (I use the extension .icpp for 'included' implementation). This file is #included at the bottom of the header file (inside the #ifdef guard), keeping the .hpp file free from all implementation details. While not required by the compiler, the header file does contain documented declarations of all template and inline functions. Personally I never write 'implicitly' inline functions where the function definition is enclosed within the class definition.

So for all classes, types and functions that are used by more than one .cpp file, the class definitions, type definitions and function declarations go in the header file, and the corresponding function definitions (implementations) go in the .cpp file, except definitions of inline and template functions go in the .icpp file. I successfully avoid global data so I don't have any rules for that. I usually end up using static member data instead, accessed through global or static member functions, and the guidelines above apply.

Classes, types and functions that are used within one .cpp file only I sometimes put in the .cpp file. These will always be simple utility functions that have very low probability of reuse elsewhere. These go in an anonymous namespace, shielding them from name collisions with other types and functions in other .cpp files.

As to private data, conceptually the users of some class has no business knowing what private data (or functions for that matter) a class has. However, C++ syntax requires these to be declared as part of the class definition, hence they do appear in the header file. This is not usually a problem, but if it is the Pimpl design pattern can be used as a work-around. Here you define two classes, Foo and FooImpl, where FooImpl holds all your data, and Foo holds an instance of FooImpl. FooImpl.hpp is not #included in Foo.hpp, thereby hiding implementational details from the users of Foo.)

0

Header files commonly contain forward declarations of classes, subroutines, variables, and other identifiers.

  • header contains generally the `class`es itself; not only forward declarations. – iammilind Jul 22 '11 at 03:49
  • As Rasmus Storjohann said, it is much more efficient to use forward declarations in header files. – Pougnet Jul 22 '11 at 05:53
  • A header file containing just `class A;` is not very useful if you want other files to be able to create instances of A. - What Rasmus means is that you prefer `class B; class A { B* b_ptr; ... };` over `include "b.hpp" class A { B* b_ptr; ...};`. He doesn't mean that the definition of class A does not belong in a header. – visitor Jul 22 '11 at 07:51