Jump to content
  • Advertisement
Sign in to follow this  
thedodgeruk

Circular Dependencies

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

If you intended to correct an error in the post then please contact us.

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 this post


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

Share this post


Link to post
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
(just first link found on google).

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!