1

I'm working on a C++ project where I've implemented every class as a separate .h file and .cpp file. I'm finding out this wasn't worth the hassle. - I'm editing back and forth between two files - It caused me a lot of unsuspected headaches (having to add predeclaration, explicitly export templated classes) - I don't see a direct benefit, my code base is bound to stay relatively small (say < 10,000 lines of code) and compilation time isn't substantial.

My question is two fold

a) Is there a benefit I might have missed in keeping implementation and prototype separated?

b) If not, is there any free tool or ide that has the capability of merging a cpp file back into the header file?

6
  • 2
    You might have missed the fact that clients can't view your implementation. Commented Jun 27, 2012 at 19:42
  • Presumably there was also hassle from having a class per cpp/hpp. Good practice in C++ allows grouping multiple related classes together in their own hpp/cpp pair. Commented Jun 27, 2012 at 19:44
  • If having multiple headers and code files is entirely too much trouble, you should probably just put all your code in a single file. After all, any editor these days will load 10kloc no problem. /sarcasm Even the temptation to do this is a fundamental misunderstanding of how the compilation process works. Commented Jun 27, 2012 at 19:45
  • Looking at the definition, or at least some breakdown, of compilation units and the stages of compilation (preprocessor, which will handle some of the includes, compiler, linker), will very quickly show why this is a Very Bad Idea. As Jonathon noted in his answer, #include literally includes the text of the file into the other file. If you're using MSVC, there is an option to retain the preprocessor outputs (*.i), which will shed even more light on this. Commented Jun 27, 2012 at 19:57
  • 1. cat *.h >> abc.cpp 2. cat *.cpp >> abc.cpp 3. g++ abc.cpp :P Commented Jun 27, 2012 at 20:06

1 Answer 1

10

Don't do this. For most simple classes, where you want to expose the class for use somewhere else, you should always* have the declaration of the class in a .h header file, and the definition (code) in a .cpp file.

You then include the .h file wherever you want to use the class (ie. instantiate it). However, the linker is responsible for "linking" the code up after each .cpp is compiled separately.

My question for you would be, "how else would you do it?" One mistake I see many n00bs make is to #include "foo.c. Then what happens is, not only do you lose the independent compilation, you run into problems at link-time because there are multiple definitions of the same class. Remember, when you #include a file, it literally plops in the body of that file where you say.

[*] There are of course exceptions to this rule:

One exception to this is for template classes. These have to be in a header file in their entirety. The reason is because the actual code is not generated until the class is instantiated with a type parameter. Then, the compiler essentially "fills-in" the body of the class with the specified type where required. Since the type is not known until then, it cannot be compiled independently and linked-up later.

Sign up to request clarification or add additional context in comments.

8 Comments

Sometimes you want classes that are only local to one cpp file. This can be for simplicity (no other file needs it) or in order to hide implementation/complexity. So you should not always have the declaration in the header file - only if you want to expose the class to use it elsewhere.
@AlexWilson Sometimes you need to have the declaration in the header even without intent to use it elsewhere; pimpl is an example of that case (and a common reason for class-within-cpp).
@peachykeen Absolutely. I agree entirely. I was just making the point that you should not always, without question, put your declaration in a header - which was how this post first read.
@AlexWilson And I would, in turn, agree with that. You should not always declare in the header.
@JonathonReinhart Thanks for incorporating the suggestions too.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.