Rule Definition
Circular references between header files can lead to difficulties during the program compilation (or can even prevent the compilation).
Remediation
The best answer to this problem is to reconsider the architecture of the program. Sometimes you can use forward declarations to avoid circular references. Using pointers instead of the class allows this to be achieved more easily.
Violation Code Sample
// FILE C1.H
#ifndef C1_H
#define C1_H
#include "C2.h"
class C2;
class C1
{
public:
C1();
C2 x;
};
#endif
// FILE C2.H
#ifndef C2_H
#define C2_H
#include "C1.h"
class C1;
class C2
{
public:
C2();
C1 x;
};
#endif
Fixed Code Sample
// FILE C1.H
#ifndef C1_H
#define C1_H
class C2;
class C1
{
public:
C1();
C2 * x;
};
#endif
// FILE C2.H
#ifndef C2_H
#define C2_H
class C1;
class C2
{
public:
C2();
C1 * x;
};
#endif
Related Technologies
C++
Technical Criterion
Architecture - Multi-Layers and Data Access
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.