Software Design
Published in

Software Design

Rules for managing header file includes in C++

C++ header file management can be very frustrating:

  • Changing the order of header files often results in difficult to track compilation errors
  • C++ project compilations take a long time due to complex header file inclusions

Here are a few rules to remedy this:

An example illustrates the header file inclusion rules:

Header file

#ifndef _a_h_included_
#define _a_h_included_
#include "abase.h"
#include "b.h"

// Forward Declarations
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

Source file

#include "a.h"
#include "d.h"

void A::SetC(C* c)
{
m_c = c;
}

C* A::GetC() const
{
return m_c;
}

void A::ModifyD(D* d)
{
d->SetX(0);
d->SetY(0);
m_d = d;
}

For details, refer to C++ Header File Include Rules

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store