Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Reduce the number of #include files in header files. It will reduce build times. Instead, put include files in source code files and use forward declarations in header files.

I read this in here. http://www.yolinux.com/TUTORIALS/LinuxTutorialC++CodingStyle.html.

So it says if a class (class A) in header file does not need to use the actual definition of some class (class B). At that time we can use the forward declaration instead of including the particular (class B) header file.

Question: If the class (class A) in the header if doesn't use actual definition of a particular class (class B), Then how forward declaration helps to reduce the compile time?

share|improve this question

4 Answers 4

The compiler does not care if class A uses class B. It only knows that when class A is compiled and it has no prior declaration of class B (forward declaration or otherwise), it panics and marks it as an error.

What is important here is that the compiler knows you didn't try to compile a program after your cat walked on your keyboard and created some random letters which may or may not be interpreted as a class.

When it sees an include, to be able to use any information contained within, it must open the file and parse it (regardless of whether or not it actually needs to do so). If that file then includes other files, those too must be opened and parsed, etc. If this can be avoided, it is generally a good idea to use a forward declaration instead.

Edit: The exception to this rule being precompiled headers. In this case, all headers are compiled and saved for future compilations. If the headers don't change, the compiler can smartly use the precompiled headers from previous compilations and thus cutting down compile times, but it only works well if you don't often need to change headers.

share|improve this answer
    
Thank you for the explanation. Then ok as example you think there are three header files vehicle.h, bus.h, toybus.h. vehicle.h include by bus.h and bus.h include by toybus.h. so if I do some change in bus.h. does the compiler open and parse the vehicle.h again? does it compile it again? –  Nayana Adassuriya Apr 23 '13 at 8:07
    
@NayanaAdassuriya Yes, it gets included and parsed each time, which is also why you see #pragma once or #ifndef __VEHICLE_H_ type declarations in header files in order to prevent such files from being included multiple times (or being used multiple times at least in the case of ifndef). –  Neil Apr 23 '13 at 8:12

Remember, C/C++ preprocessor is a separate, purely textual, processing step. The #include directive pulls in the content of the included header and the compiler has to parse it. Moreover, compilation of each .cpp is completely separate, so the fact that the compiler just parsed B.h when compiling B.cpp does not help it the least when it needs it again when compiling A.cpp. And again when compiling C.cpp. And D.cpp. And so on. And each of those files have to be recompiled if any file included in it has changed.

So say class A uses class B and classes C and D use class A, but don't need to manipulate B. If class A can be declared with just forward-declaration of B, than B.h is compiled twice: when compiling B.cpp and A.cpp (because B is still needed inside A's methods).

But when A.h includes B.h, it is compiled four times—when compiling B.cpp, A.cpp, C.cpp and D.cpp as the later two now indirectly include B.h too.

Also when header is included more than once, the preprocessor still has to read it each time. It will skip processing it's content because of the guarding #ifdefs, but it still reads it and needs to search for the end of the guard, which means it has to parse all preprocessor directives inside.

(As mentioned in the other answer, precompiled headers are attempt to work around this, but they are their own can of worms; basically you can reasonably use them for system headers and only if you are not using too many of them, but not for headers in your project)

share|improve this answer
    
+1, the header-includes become only a serious problem when you have a fairly huge number of classes, not when having just two classes A and B. All other posts seem to miss that central point. –  Doc Brown Apr 23 '13 at 12:24

A forward declaration is much faster to parse than a whole header file that itself may include even more header files.

Also, if you change something in the header file for class B, everything including that header will have to be recompiled. With a forward declaration, that may only be the source file where A's implementation is residing in. But if A's header actually included B's header, everything including a.hpp will be recompiled also, even if it doesn't use anything of B.

share|improve this answer

because then A.hpp doesn't need to #include B.hpp

so A.hpp becomes

class B;//or however forward decl works for classes

class A
{
    B* bInstance_;
//...
}

so when A.hpp is included then B.hpp is not implicitly included and all files which depend only on A.hpp don't need to be recompiled each time b.hpp changes

share|improve this answer
    
but in the source file (A.cpp). need to include the actual header file (B.h). So every time it needs to compile. Finally both way B.h need recompile with the changes. Any different? –  Nayana Adassuriya Apr 23 '13 at 7:58
    
@NayanaAdassuriya no because A only uses a pointer to B and changes to B won't affect A.hpp (or the files that include it) –  ratchet freak Apr 23 '13 at 8:32
    
@NayanaAdassuriya: Yes, A.cpp will have to recompile (if it uses the definition of B inside bodies of A's methods, but it usually does), but C.cpp, which uses A, but not B directly, will not. –  Jan Hudec Apr 23 '13 at 11:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.