Don't know about hpp for templates. It's *.h or *.hpp for everything
if you ask me. If there is only one class ion the file, or if it's
clearly the "main" class in it, then class_name.h is only natural.
Maintainability: if the general rule that each class "class_name" is
defined in class_name.h and implemented in class_name.cpp, navigating
the code is easier than if 7 different classes are defined in file1.h
and randomly implemented in file2.cpp and file3.cpp and some additonal
classes are defined and implemented entirely in file3.cpp
Disagreed. There's classes that will only be used in one translation
unit. What can one gain by making another trivial header?
I don't entirely disgree with you. but in large project, this can be a
valid policy.
Typically, what will happen is devA decides he needs a little helper
class for his big class, he knows he only needs it here in this
specific translation unit so just define and implement it fully inside
big_class.cpp.
Then often, devB, who is implementing another_class, needs some helper
functionality. It's fairly generic functionality so he peeks in the
generic area for a header file that might indicate that this
functionality is already available. He doesn't see anything so he
implements my_helper_class inside another_class.cpp ith functionality
almost to same as that implemented by devA previously.
Then so on for DevC, DevD ...
If you are lucky, maybe DevE actually finds the implemention that
exists in big_class.cpp and figure out that he should reuse it rather
than duplicate the functionality. Unfortunately, for him to reuse it,
he now has to extract first_helper out of big_class.cpp, create
first_helper.h and first_helper.cpp.
If instead DevA wrote first_helper in its own files, any future reuse
would have been much easier.
In a previous life, I once searched through the code base of a large-ish
C project and found something like 4-5 different implementation of a
XML-quoting function, all of them defined and implemented locally.
Then there's unit testing: if your class is only defined inside a a
cpp file, it's rather difficult to unit test it.
More important in my opinion:
For every header file the following should compile without error or
warning:
#include "the_header.h"
int main() { return 0; }
if you need to preceed #include "the_header.h" by anything, your
header file is not self contained and should be fixed.
Yannick