[C++] Cyclic dependency

Started by
2 comments, last by Vanderry 14 years ago
Hello GameDevs ! I often feel like I want two classes in different source files to be dependent on each other and although I realize this might be a bad design I wanted to really ask for some professional impressions on the subject. In my latest case there are two classes CPlayer and CAbility and they need to process information about each other in order to update properly (through pointer references). I know I could gather them up in the same source file but that would kind of defeat the purpose of splitting source code up. It seems to me at this point that if I forward declare CAbility in its header file before including "CPlayer.h" the program at least compiles without a problem. I would like to know if this is a safe solution or extremely inadvisable and if so is there ever a case where it might be a good practice? Many thanks for any input on this !
Advertisement
It's perfectly safe and the preferred way of doing things.
<iosfwd> for example forward declares iostreams and the like, so you don't have to include the beast <iostream> if all you wanted was to add support for the << stream operator.
Quote:Original post by Vanderry
Hello GameDevs !

I often feel like I want two classes in different source files to be dependent on each other and although I realize this might be a bad design I wanted to really ask for some professional impressions on the subject.

The only correct answer one can give is: "It depends."
Quote:In my latest case there are two classes CPlayer and CAbility and they need to process information about each other in order to update properly (through pointer references). I know I could gather them up in the same source file but that would kind of defeat the purpose of splitting source code up.

Ignoring your naming scheme, do they really need to know about each other? What does the player need to know about the ability other than it exists and that they possibly have a pointer to it somewhere? In that case a simple forward declaration is more than sufficient for the player. On the ability's side of things, I can see it needing information from the player (such as the amount of current hit points the player has if the damage the ability does is dependent upon that, etc.
Quote:It seems to me at this point that if I forward-declare CAbility in its header file before including "CPlayer.h" the program at least compiles without a problem.

Generally, when your class is just storing a pointer or reference to an object, and you are declaring the class in a header and implementing in a separate compilation unit, you should prefer forward declarations over including the headers. This reduces overall dependency issues, and can speed up compilation.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Thanks guys, that's just what I was hoping to hear ^^

This topic is closed to new replies.

Advertisement