Ivan said:
Hi,
if I have the choice between puting a class in .h file or in .cpp file,
which one is more efficient?
Efficient in respect to ... ?
Some nitpicking: you probably meant class definition.
I prefer putting in .h file because the compilation and linking are
easier since I don't need to include so many .cpp files in the command.
You really should use Makefiles or the project managment system of your
favourite IDE. They exist for a reason. (I think you just discovered
it...)
But my guess is that there is a drawback though I don't know what
exactly.
Depending on compiler, project size, use of diverse features and the
like the compilation time may suffer between a little and very much. It
should not really matter for the resulting code.
It's usually said to be good practize keeping compilation units as small
as possible, and always just exhibiting as little information as
necessary (i.e., use forward declarations where possible, use only
interfaces, use only class declarations, and as last resort, put the
whole thing into a header (sometimes necessary with templates)).
This has usually the benefit of much faster compilation, the benefit
getting bigger as your project grows. If you use some really template
heavy stuff for just one of your classes (for instance boost::spirit),
compilation times can get unacceptable, even for small projects.
Additionally, putting everything into one place opens the door to bad
tricks. You probably will fail on some point to recognize certain
unnecessary depencies, which will cost time when you want to reuse your
code later, in another environment. And as we are humans, somebody
usually will start to rely on specific implementation of the other
classes interface (as they are always completly available), which can
(and will) lead to problems after a few months (because we all document
all our assumptions, don't we?).
best regards,
-- Markus