Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any good reason for C++ header file to not include any other header files?

I've seen a header include style like this, where header files don't include other header files and the corresponding *.cpp files must include all the dependencies (and include them in the right order). It seems possible that in the good old days that this would make build dependency tracking easier (but I'm just guessing). Is there a good reason for it nowadays?

File "B.h":

#ifndef _B_h_
#define _B_h_

// Note we do not #include "A.h" that contains class A declaration.

class B
{
public:
   A a; // An A object.
};
#endif // _B_h_

File "B.cpp":

#include "A.h" // Must include this before B.h, otherwise class A not defined in B.h
#include "B.h"

...
like image 303
user79878 Avatar asked Aug 03 '11 23:08

user79878


People also ask

Should headers include other headers?

Implementation. This rule means that if the header uses a type — such as ' FILE * ' or ' size_t ' - then it must ensure that the appropriate other header ( <stdio. h> or <stddef. h> for example) should be included.

Can a header file include another header file?

The answer is that yes, it can; header files (that is, #include directives) may be nested. (They may be nested up to a depth of at least 8, although many compilers probably allow more.) Once funcs. h takes care of its own needs, by including <stdio.

Is it necessary to include header file in C?

In C program should necessarily contain the header file which stands for standard input and output used to take input with the help of scanf() and printf() function respectively.

Why should I not include CPP files?

If you #include a cpp file in several other files in your program, the compiler will try to compile the cpp file multiple times, and will generate an error as there will be multiple implementations of the same methods.


2 Answers

Yeah, that would be bad practice, because if somebody gets the order wrong they'll get errors that they may or may not be able to figure out. If all the header files have include guards, then one header including all the other headers it needs won't be a problem, which is how it should be.

like image 158
Seth Carnegie Avatar answered Nov 15 '22 06:11

Seth Carnegie


It seems whoever wrote that code misunderstood the common recommendation of reducing the number of included headers. It is usually recommended to remove unnecessary #include <> directives in the hope of accelerating compilation. Indeed, on large projects, it may accelerate compilation significantly by:

  1. reducing the number of header files the compiler needs to open to compile any given source file;
  2. reducing the number of source files that need to be recompiled after a header changes.

In general, people will recommend (its been on coding standards for all projects I've worked on) using forward declarations for classes unless the class defined in the concerned header is:

  1. used as a base class;
  2. used as a data member;
  3. has an incomplete official speciifcation (e.g. standard library containers are allowed to have extra template arguments as long as they have defaults, so it's non-standard to forward declare them).

In cases 1 and 2, the #include <> directive must appear before the class definition in all dependent source files and headers. Basically, it just moves the #include <> directive(s) from the header into each of its dependencies. It results in more code and does so with no benefit (e.g. compilation time etc. will be the same). For this reason, this recommendation is also accompanied by another entry in the coding standard: each header file should compile "stand alone" (e.g. included on the first line of a source file).

like image 43
André Caron Avatar answered Nov 15 '22 07:11

André Caron