4. Header File Inclusion Rules
• The inclusion mechanism should be tolerant to
duplicate header file inclusions.
• A header file should be included only when a
forward declaration would not do the job.
• The order of header file inclusion is not
important.
1-4
5. Rule 1: Be tolerant to duplicate
header file inclusions.
/*
* Header file name: estimate_function.h
*/
#ifndef ESTIMATE_FUNCTION_H
#define ESTIMATE_FUNCTION_H
#include <iostream>
using namespace std;
double estimateOfTotal(int minPeas, int maxPeas,
int podCount);
#endif
1-5
6. Rule 2: Use forward declaration
when possible.
Suppose we want to define a new class B that uses objects
of class A.
class B {
private:
A* fPtrA ;
public:
void myMethod(const& A) const ;
};
1-6
#include <A.h>;
B only uses references or pointers to A. Use forward
declaration then: you don't need to include <A.h>. This
will in turn speed a little bit the compilation.
class A;
7. When we need to include <A.h>?
• B derives from A or B explicitely (or implicitely) uses
objects of class A. You then need to include <A.h>
class B : public A {
} ;
class C {
private:
A fA ;
public:
void myMethod(A par) ;
};
1-7
#include <A.h>;
8. Exercise 1: Header Files
• Assume a class A with code stored in a.cpp and a.h.
#ifndef A_H
#define A_H
#include "abase.h"
#include "b.h"
// Forward Declarations/definition
class C;
class D;
class A : public ABase {
B m_b;
C *m_c;
D *m_d;
public:
void SetC(C *c);
C * GetC() const;
void ModifyD(D *d);
};
#endif 1-8
Editor's Notes
Reference: http://stackoverflow.com/questions/4757565/c-forward-declaration
How forward-declarations can significantly reduce build times
You can get the declaration of a function into your current .cpp or .h file by #includ&apos;ing the header that already contains a declaration of the function. However, this can slow down your compile, especially if you #include a header into a .h instead of .cpp of your program, as everything that #includes the .h you&apos;re writing would end up #include&apos;ing all the headers you wrote #includes for too. Suddenly the compiler has #included pages and pages of code that it needs to compile even when you only wanted to use one or two functions. To avoid this, you can use a forward-declaration and just type the declaration of the function yourself at the top of the file. If you&apos;re only using a few functions, this can really make your compiles quicker compared to always #including the header. For really large project the difference could be an hour or more of compile time bought down to a few minutes.
Reference: http://stackoverflow.com/questions/4757565/c-forward-declaration
How forward-declarations can significantly reduce build times
You can get the declaration of a function into your current .cpp or .h file by #includ&apos;ing the header that already contains a declaration of the function. However, this can slow down your compile, especially if you #include a header into a .h instead of .cpp of your program, as everything that #includes the .h you&apos;re writing would end up #include&apos;ing all the headers you wrote #includes for too. Suddenly the compiler has #included pages and pages of code that it needs to compile even when you only wanted to use one or two functions. To avoid this, you can use a forward-declaration and just type the declaration of the function yourself at the top of the file. If you&apos;re only using a few functions, this can really make your compiles quicker compared to always #including the header. For really large project the difference could be an hour or more of compile time bought down to a few minutes.
Reference: http://stackoverflow.com/questions/4757565/c-forward-declaration
How forward-declarations can significantly reduce build times
You can get the declaration of a function into your current .cpp or .h file by #includ&apos;ing the header that already contains a declaration of the function. However, this can slow down your compile, especially if you #include a header into a .h instead of .cpp of your program, as everything that #includes the .h you&apos;re writing would end up #include&apos;ing all the headers you wrote #includes for too. Suddenly the compiler has #included pages and pages of code that it needs to compile even when you only wanted to use one or two functions. To avoid this, you can use a forward-declaration and just type the declaration of the function yourself at the top of the file. If you&apos;re only using a few functions, this can really make your compiles quicker compared to always #including the header. For really large project the difference could be an hour or more of compile time bought down to a few minutes.