Public Group

Circular Dependencies

This topic is 3006 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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

Share on other sites
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.

Share on other sites
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

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 10
• 10
• 14
• 10
• 25
• Forum Statistics

• Total Topics
632647
• Total Posts
3007640
• Who's Online (See full list)

There are no registered users currently online

×