teddy
teddy

Reputation: 171

Is it legal to split the definition of classes defined in the same header across multiple source files?

In c++ are you allowed to split the definition of classes defined in the same header file across multiple source files? For instance if I have a file that defines the classes Quad2 and Quad3, can I define class Quad2 in a file called Quad2.cpp and Quad 3 in a file called Quad3.cpp but still have both in the same header file Quad.h?

Would this be considered illegal?

Upvotes: 5

Views: 277

Answers (4)

Marcelo Cantos
Marcelo Cantos

Reputation: 186068

Yes, you can implement different classes from the same header file in different implementation files.

Header files are nothing more than textual content that gets injected into the source at the time of compilation. The compiler proper has absolutely no conception of a header. Headers are resolved in the preprocessor, before the compiler sees anything.

There is thus no requirement at all about which files a class's definition and implementation reside. The norms about what belongs in header files and what belongs in implementation files have everything to do with best-practice, and nothing to do with rules enforced by the compiler. I.e., you put class definitions in headers because that makes them easy to inject into both the implementation of that class and any code that wants to use the class.

Upvotes: 5

Mats Petersson
Mats Petersson

Reputation: 129524

Yes, perfectly legal. In fact, you can have Quad2A.cpp and Quad2B.cpp if you like to split the code into a smaller file (of course, it may not be of any benefit to do this).

One drawback with separating the sources, however, is that you make it harder for the compiler to inline functions that are small [or only used once]. Most compilers will only inline functions that are in the same compile unit. So if Quad3.cpp is using something from Quad2.cpp, then it's possible that it could be inlined if they are in the same file, but won't be if they are in separate files. However, this should not really be a deciding factor for your design - just mentioning it as one of the side-effects of "lets split this".

Upvotes: 2

rectummelancolique
rectummelancolique

Reputation: 2247

Just try it and see for yourself! But no, it is not illegal. An approach that would confuse people a bit less (IMHO) would be to have Quad2.h and .cpp, Quad3.h and .cpp and include both Quad2.h and Quad3.h in Quad.h for convenience.

Nothing forces you to, but try to keep the pattern X.h associated with X.cpp, it's common practice.

Upvotes: 1

Ralph Tandetzky
Ralph Tandetzky

Reputation: 23650

That totally valid. You can distrubute your function definitions wildly across different source files. The linker will put everything together and complain, if it doesn't find a definition.

Remember: Writing #include <some_header.h> is like copying the complete content of the header into the source file in that place. If you include Quad2.h and Quad3.h in both source files Quad2.cpp and Quad3.cpp then this will have exactly the same effect, as if you had put both headers into one file Quad.h and included that instead in the cpp files. Using pairs of header and implementation files is just a matter of convention (and it's a good one).

Upvotes: 1

Related Questions