.cpp and .h files

Jun 6, 2011 at 9:08pm
From what I can understand, it seems, by convention, a class would be defined in a .h file and implemented in a .cpp file. The programs that would use these classes would have to #include the .h file.

1) is this correct?
2) does the class.h have to #include class.cpp?
and finally, I know a whole class can be implemented in a .h file, so
3) why are classes written and implemented seperately?

Thanks in advance, this is my first post on this site.
Jun 6, 2011 at 9:12pm
- That seems correct yes.

- No, as a general rule do not include .cpp files.

- This is pretty much just to keep organized.
Jun 6, 2011 at 9:38pm
Regarding 3:
Perhaps this is off the point, but also if you implement class member functions inside the class, they are counted as inline, which is not good for larger methods.
Last edited on Jun 6, 2011 at 9:38pm
Jun 6, 2011 at 9:39pm
@ Xander314: That sounds like it would be compiler specific, can you cite a reference?
Jun 6, 2011 at 9:51pm
This website's classes tutorial says:

The only difference between defining a class member function completely within its class or to include only the prototype and later its definition, is that in the first case the function will automatically be considered an inline member function by the compiler, while in the second it will be a normal (not-inline) class member function, which in fact supposes no difference in behavior.



It's also on MSDN and IBM (not necessarily standard, I know) and on a C++ Wikibook:
http://msdn.microsoft.com/en-us/library/bw1hbe6y(v=vs.80).aspx
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Finline_member.htm
http://en.wikibooks.org/wiki/C++_Programming/Classes/Member_Functions
Last edited on Jun 6, 2011 at 9:52pm
Jun 6, 2011 at 10:00pm
Well I'm convinced. That's a good thing to know, thank you.
Jun 6, 2011 at 10:02pm
The Standard wrote:
7.1.2.3. A function defined within a class definition is an inline function. [...]


@Xander314
Just because a function is inline, doesn't mean calls to it are actually getting inlined.
That's entirely up to the compiler, which will inline only in situations where it thinks it's a good idea (even if a function is not declared inline).
Jun 7, 2011 at 8:12am
@Athar I was aware that the compiler the final say over this feature. I didn't know it could inline functions not marked as inline. Doesn't that make inline completely redundant?
Last edited on Jun 7, 2011 at 8:12am
Jun 7, 2011 at 8:19am
Yes, pretty much.
It's still needed when defining non-template functions in headers to avoid multiple definitions, but aside from that the compiler just does what it wants anyway (which is a good thing, of course).
Jun 7, 2011 at 8:56am
The only difference between defining a class member function completely within its class or to include only the prototype and later its definition, is that in the first case the function will automatically be considered an inline member function by the compiler, while in the second it will be a normal (not-inline) class member function, which in fact supposes no difference in behavior.


as i thought too, i guess there could be no difference between defining the function member within a class or outside the class. it should have the same effect...

@question number 2: it's reversed. the .cpp file include the .h file...

note: CMIIW :)
Jun 7, 2011 at 3:36pm
@Athar Does that even include functions in static libraries (i.e. that have already been compiled)?. I think I read somewhere that modern optimizing compilers do tend to talk to the linker these days...
Last edited on Jun 7, 2011 at 3:37pm
Jun 7, 2011 at 5:55pm
Traditionally, the compiler needs to be able to see the definition of the function to be inlined, which is usually only the case when it is defined in a header.
But you're right, modern compilers support link time optimization, so they are able to inline a function from a static library if the static library was compiled with LTO enabled and you compile and link your program with LTO enabled as well.
Jun 7, 2011 at 5:57pm
What about if it's defined in one of the source files of the project? Could the compiler choose to inline that before compiling it, or would it only be possible at link time via LTO?
Last edited on Jun 7, 2011 at 5:58pm
Jun 7, 2011 at 6:19pm
Since each translation unit is compiled independently from each other, the compiler can't see function definitions in other translation units at compile-time, so the inlining won't happen at that stage.
By its nature, the linker always knows which translation units the program consists of and so this allows the sort of inter-procedural optimizations that the compiler wasn't able to do.
Jun 7, 2011 at 6:26pm
Thanks. It's always good to learn new C++ technicalities ;)
Topic archived. No new replies allowed.