Circular Dependencies

Started by
1 comment, last by Orione 13 years, 9 months ago
i have a problem of Circular Dependencies


class A
#include "B.h"


and

Class B
#include "A.h



to solve would i write a seperate class , C, is this way or another way please

class A
#include "C.h"

Class B
#include "C.h
Advertisement
It depends on what you are doing exactly.

Why do they need to include the header containing the other class?

Do they both just have pointers to the other one? If thats the case, you can just forward declare the class and not have to include the header file.

For example...

class B;class A{    B* m_b;};


class A;class B{    A* m_a;};


If the problem is they both require something else that is in each header file, such as some enumerations, the solution may be to split the enumerations to a third file or to redesign the classes to not have the dependency on each other.
Shoot Pixels Not People
Which language? I presume it is C++.

What are the reason for you need class A to include class B and viceversa?

If your class A needs to include instances of class B and the same in class B you can have these instances declared as pointer in class definition, use class forward as explained in
http://www-subatech.in2p3.fr/~photons/subatech/soft/carnac/CPP-INC-1.shtml
(just first link found on google).

If this is not the case please show us some code to better help you.

This topic is closed to new replies.

Advertisement