Archived

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

Cyclic(?) members in classes.

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

Say I have the following headers:
  
//BOB.H

#ifndef BOB_H
#define BOB_H

#include "Doug.h"

class CBob{
   public:
     CDoug Doug;
};

#endif
  

  
//DOUG.H

#ifndef DOUG_H
#define DOUG_H

#include "Bob.h"

class CDoug{
   public:
     CBob Bob;
};

#endif
  
How can one make this work? My problem is that my Tank class utilizes my CParticleList class, and the CParticleList class has to have a member CTank *Tanks to effect damage on the tanks from particle hits. Can I have it set up like I want, or do I have to structure something differently?
-=skjin=-

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Forward declaration is your friend here. In class Bob, instead of #include "Doug.h", just say:
class CDoug;

That way Bob knows there is a Doug but you don''t run into the circular dependency.

Share this post


Link to post
Share on other sites
You could use forward declaration in one of the files:


            
//BOB.H

#ifndef BOB_H

#define BOB_H

//#include "Doug.h" //don't need that anymore.



class Doug;//forward declare Doug. In the implementation file (.cpp) you can include Doug.h

class CBob {
public:
CDoug Doug;
};
#endif





[edited by - bslayerw on January 29, 2003 5:26:44 PM]

Share this post


Link to post
Share on other sites