Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


circular dependencies

This topic is 5798 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

At the moment I am thinking about implementing a scene manager which will take care of everything that has to do with graphics. But now there is the problem of circular dependencies. My map class (declared in map.h2 has to know the scene manager class in order to be its "friend" but on the other hand the scene manager has to know the map class in order to deal with maps. The same problem occurs for models, sprites and the likes. So how can I implement a dualistic relationship between the scene manager and the rest?

Share this post

Link to post
Share on other sites
Optimally you''d try to avoid situations in which circular dependance is necessary but it''s sometimes inevitable. I imagine redesigning isn''t going to help here.

So you have to do a forward declaration.

In the header file for the map class add this above the class declaration:
class CSceneManager;
(of course assuming that''s the name of your class)

And in the scene manager''s header, add this:
class CMap
(again assuming a name)

And in each header, do not include the header for the other class. The forward declaration is enough (unless the header has code in it that accesses members of the other class, in which case move that code to the accompanying cpp file). You do however have to include both headers in the file where the methods are defined.

Next, you have to ensure that both classes only contain pointers to each other. If they''re actually instances they''ll recur infinitely and it probably won''t compile. Similarly, don''t have each class new the pointer for the other in the constructor. The same thing happens but instead of compile errors, your program will just freeze. Newing pointers in other methods is okay, just keep track of who should delete who.

Hope I got that all right...


Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!