We define a C++ class in a .h and define its methods in a .cpp, but it makes the code look less organized.
I want to put all method’s definition in the class definition which is in a .h file, but I’m worrying that the compiler generate duplicated code for the same methods/functions when one class header file is included by different files.
Does the linker find out and merge the duplicated code pieces to reduce the file size?
If not, is it better to use .hpp instead? I heard that a .hpp is for this.
And it does make minor difference when I just change a .h file for a .hpp (I don’t know why), compiled with G++.
.h vs .hpp is the 90% equivalence of
Some people prefer to use
.hppwhen they are doing exclusive C++ programming. You will see.hppin libraries likeBoost.However, the other 10% is really important. For example, taking from
Boostlibrary doc, they explain the reason of using.hppover.h:If you fall in that case, you should use
.hpp, but this can cost longer compilation time. Otherwise, you might want to keep.hstyle. That’s just my personal taste. It isn’tC-orientedat all, in my honest opinion.Further reading:
Splitting templated C++ classes into .hpp/.cpp files–is it possible?
Condensing Declaration and Implementation into an HPP file
C++ templates declare in .h, define in .hpp