Originally Posted by
CornedBee
NO! NEVER NEVER NEVER include cpp files!
Placing all code in the header, or including the cpp is exactly 100% the same thing. Of course both cases can only be done when using templates, otherwise you'll get symbol redefinition. When using templtes I ALWAYS place all code in a cpp then include it, because then I'll get a clean headers, only with declaration, and comments explaning everything. But there's a vey important issue when including a cpp
In the h file I write
Code:
#ifndef _FILE_H_
#define _FILE_H_
/*
declarations, comments, whatever
*/
#include "file.cpp"
#endif //_FILE_H_
in the cpp I write
Code:
#ifdef _FILE_H_
/*
templated code, inline functions
- the part is compiled only when included from the h
*/
#else
#include "file.h"
/*
non-templated code
*/
#endif //_FILE_H_
This way each piece of code won't be analysed twice, and you can make sure the matching header has been included.
during compilation, the directive #include "file.cpp" will be substituted by the contents of the cpp file, which is the same as placing all code in the h.
But again, it's up to style. I much prefer having clean simple, and commented headers (I believe that's their porpouse), than all code mashed together.
So you decide. This also applies to inline functions