Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forward declaration / when best to include headers?

I'm pretty clear on when I can/can't use forward declaration but I'm still not sure about one thing.

Let's say I know that I have to include a header sooner or later to de-reference an object of class A. I'm not clear on whether it's more efficient to do something like..

class A;
class B
{
   A* a;
   void DoSomethingWithA();
};

and then in the cpp have something like..

#include "A.hpp"
void B::DoSomethingWithA()
{
   a->FunctionOfA();
}

Or might I as well just include A's header in B's header file in the first place? If the former is more efficient then I'd appreciate it if someone clearly explained why as I suspect it has something to do with the compilation process which I could always do with learning more about.

like image 293
Holly Avatar asked May 06 '12 09:05

Holly


People also ask

When Should header files be used?

Header File Inclusion Rules A header file should be included only when a forward declaration would not do the job. The header file should be so designed that the order of header file inclusion is not important. The header file inclusion mechanism should be tolerant to duplicate header file inclusions.

Should I use forward declaration or include?

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.

Where do you put forward declarations?

Generally you would include forward declarations in a header file and then include that header file in the same way that iostream is included.

When the forward declaration is required while declaring function?

Forward declaration is used in languages that require declaration before use; it is necessary for mutual recursion in such languages, as it is impossible to define such functions (or data structures) without a forward reference in one definition: one of the functions (respectively, data structures) must be defined ...

Why use forward declaration instead of header file?

- it's good practice to use forward declaration instead because you eliminate redundant dependencies by using it. Also note, that when you change the header file, it causes all files that include it to be recompiled.

When should I avoid including a header in a file?

If you can avoid including that header, then avoid it. - it's good practice to use forward declaration instead because you eliminate redundant dependencies by using it. Also note, that when you change the header file, it causes all files that include it to be recompiled.

What is the purpose of the forward declaration of a class?

The forward declaration tells the compiler that class A exists without describing what it looks like; this is adequate for defining a pointer or a reference. When it comes time to use that pointer or reference, the complete class definition will be required.

Why do some headers take longer to compile than others?

Any file that includes header file will also include its includes. If you have an header that includes a rather expensive file, then those that also include that header will also take longer to compile. If you move the expensive include to the implementation, and forward declare in the header, then only the source file will take longer to complile.


1 Answers

Use forward declarations (as in your example) whenever possible. This reduces compile times, but more importantly minimizes header and library dependencies for code that doesn't need to know and doesn't care for implementation details. In general, no code other than the actual implementation should care about implementation details.

Here is Google's rationale on this: Header File Dependencies

like image 66
irobot Avatar answered Sep 21 '22 00:09

irobot