Hello , I always had a distant relationship with C++ (writing in it small programs (or libs for other languages) only when performance really matters) but I have found myself writing more and more C++ bots. I learned that you need to separate the definition of the class (interface in c++ ) in a hpp file with its implementation in a cpp file , but as i write more I got tired of that and now I implement my methods in the header hpp file .
I have tried to understand why this is not the way to do it but I still don't get it. Of course implementing the methods in a cpp file could give you the option to implement some of them in one file and some of them in another in the future but that would be a mess in my opinion.
Have I got this all wrong ?

This approach only works if your header is used in only one code file. If you have two code files using the same header then each compiled object file will have a complete set of functions in it. When the linker tries to create your binary executable file it will produce errors about duplicate definitions.

There is also a benefit in being able to implement the code for different classes in different code files. If your header defines many classes then you're going to end up with a truly enormous file if the header also contains the implementation. One of the reasons for having a header file is that it provides a "summary" of the code that will be used by the other parts of your application. A header is supposed to be short, easily maintained, quickly ingested by the compiler, and rarely requires modification.

commented: Nice answer!! +15

Hello TerribleTadpole , thank you for taking the time to reply. I understand that as a basic rule what you wrote makes sense.

Here are the two reasons I never considered those two points:
I always check in my header files that they haven' already defined with a macro e.g:


using std::cout;
using std::endl;

namespace example {
class Test {
    void sayHello() {
        cout << "HELLO WORLD FROM EXAMPLE TEST" << endl;

#endif /* EXAMPLE_TEST_HPP_ */

That way i don't even have to check when I include the header file if it has included already from another class. I used to do it with a macro before including it , eg:

#include "Example/Test.hpp"

but using Eclipse CDT already implements the first way for me automatically when i create a new header file , any take what is the best or if they are equivalent ?

For the second point , I still don't understand why anyone would want to have more than one class in a hpp file. I am using an external json library that is only one hpp file with multiple classes but i guess they done it only to make it easier to put it in your project , the don't have multiple cpp files , just one hpp. I would understand to use one hpp file for multiple classes if you had to define the classes of a namespace all together , but this is not the case , and moreover my logic dictates that the classes of a namespace would be in the same folder (e.g. Example in this one ) to view immediately what is where.
Also we use IDE's so there is no need even to open a hpp to view what methods you have available and what they are doing (using comments). If I define the class in a hpp file and split the implementation in several cpp files I will be lost trying to figure out where is what.
Again thank you for taking the time to reply , and I would love to read any views you might have about what I shared. I am not asking to tell how I do it , I am asking to understand why implementing a class in a hpp file isn't considered a good practice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.