simple #include question

I should know this by now, but...

Let's you have three classes A and B and C.
For files you have A.h, A.cpp, B.h, B.cpp, C.h and C.cpp.

I want to #include B.h in my class A. Do I put #include "B.h" in A.h or A.cpp?

Finally if I've included C.h into class B and B.h is included into class A, do I have to include C.h into class A or is it there because of class B?

Thanks
An include is the same as copy-paste.
A header may include other for class definitions
Highlight the contents of your "B.h" file and copy it to your clipboard (Ctrl+C). Go to A.h and highlight the line '#include "B.h"' and hit paste (Ctrl+V). This is literally what the compiler does when processing the "#include" pre-processor command while creating .o files. This is why source files (*.cpp) have corresponding object files but header files do not.

Also you'll want to watch out for something called "Circular Dependency", it's a real PITA to prevent when you start doing stuff like this. It's often better\easier to just have all of your classes inherit from a common base class then it is to have one class contain instances of the other two.
Last edited on
Thanks guys.

I should have searched a little harder. For anyone who came to this thread looking for answers because you had the same question, go here:

http://www.cplusplus.com/forum/articles/10627/

It will answer all your questions about #include.
Topic archived. No new replies allowed.