18

Possible Duplicate:
In C++ why have header files and cpp files?

I don't quite get C++ header files, for two conflicting reasons:

  1. I thought the purpose of header files was in general to separate interface and implementation. In other words, the client uses the header file to learn how to use the class, but doesn't have to worry about how the class actually implements this functionality internally.

  2. Why, then, are private variables of a C++ class specified in its header file?

It seems to me that having private variables in the header file violate the separation of interface and implementation. Intuitively, I would think it would make more sense for private variables to be in the source file, since this is the file that is not exposed to the outside world.

Maybe I'm just misunderstanding the purpose of header files, and 1. above is just completely wrong? In what ways?

Community
  • 1
  • 1
NoobOverflow
  • 1,208
  • 3
  • 14
  • 19
  • Aside from `friend` functions, I bet it's just a technical limitation for the compiler to determine the size of the class. – Mysticial Jul 17 '12 at 17:17
  • @DaveSwersky That dupe doesn't answer the OP's second question, "Why, then, are private variables of a C++ class specified in its header file?" – Mysticial Jul 17 '12 at 17:18
  • @Mysticial: The second answer to that question addresses both points. – Dave Swersky Jul 17 '12 at 17:20
  • 12
    @NoobOverflow You're correct on point 1. And point 2 is also a good question. The answer is, for the most part, that the C++ compilation model sucks. – R. Martinho Fernandes Jul 17 '12 at 17:23
  • @DaveSwersky If it is, I don't see it. (maybe I didn't search hard enough) But the only instance of the word "private" I see is in Jalf's comment under the first answer. – Mysticial Jul 17 '12 at 17:23
  • Not a duplicate of [this](http://stackoverflow.com/q/11525574/912144), but the answers would definitely help you. – Shahbaz Jul 17 '12 at 17:24
  • 1
    2 so code using the class knows how big the class is, so it knows how much room to leave for instances it's creating on the stack etc. – Rup Jul 17 '12 at 17:24
  • just to say, in a modular (import/export) system, you do include all private information in the header (=the source) – apple apple Nov 27 '21 at 16:56

4 Answers4

13

The primary functional purpose of C++ header files is that in the C++ language there aren't module imports or anything similar that exist in other languages. The only way for the compiler to know about types, functions, etc from other files is to paste the code into the current source file by using #include.

Theoretically you could put all your source code into the header as well and just have one source file that includes all the headers. The reason this isn't usually done is twofold. First, it would take longer to compile (a significant concern on some projects) and any change to any file would result in a complete recompilation of the project. Secondly, putting the implementation into a source file does in fact help separate the interface from the implementation, even if a portion of the implementation is still specified in the header file.

Note that inline methods in headers also walk a fine line of implementation detail exposed to the public/clients of your class.

If you really wish to completely separate the interface from the implementation (which has definite merit) the C++ way to do so is to utilize the pimpl idiom. Using that idiom all the private data is hidden away in the source file and only an abstract interface is provided to the public. Additionally using the non-virtual interface (NVI) pattern can further help isolate clients from interface changes.

Mark B
  • 95,107
  • 10
  • 109
  • 188
10

C++ distinguishes between declaration and definition of functions and classes. In general a C++ header file contain the declaration of a class. Since no partial declarations are allowed the header file needs to contain the complete class declaration including all private members (variables and member functions).

If you want to hide the complete implementation from the public you can use the pimpl idiom to achieve this.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
7

The purpose of header files are to give the compiler the information it needs to share definitions between compilation units (.cpp source files). It's a mechanical thing, not a philosophy thing.

For example, private member variables need to be declared because they define the size of an object, and the compiler needs to know the size when it allocates an object.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Wouldn't be the correct term to "share declarations" between compilation units? It's confused often enough, what the means of declaration and definition are ... – πάντα ῥεῖ Jul 17 '12 at 17:55
  • @g-makulik, I was using "definitions" in the English sense of the word, not the formal C++ usage. And it is certainly possible to include definitions in the header, with templates for example it's almost mandatory. – Mark Ransom Jul 17 '12 at 18:06
  • why only private member variables? – stdout Nov 27 '16 at 18:17
  • @zgulser not *only* private member variables, that was just an example. It's really the entire declaration that's needed. – Mark Ransom Nov 27 '16 at 18:32
  • @MarkRansom Thanks Mark. Actually, I was surprised that nobody mentioned about allocation (but you) since I believe it's the main reason why C++ had to put object state in a header file to know it's size for allocations on stack. I guess that time this was the only way that they could come up with. Though Java messed it up a little bit more but putting all in a single pot. – stdout Nov 27 '16 at 19:31
0

In C/C++, one purpose of header files is to allow multiple translation units to use types with the same definition without duplicating the definition across multiple files. Part of the definition of a class is its private members.

Eric Finn
  • 8,629
  • 3
  • 33
  • 42