Archived

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

skjinedmjeet

Cyclic(?) members in classes.

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
Hey thanks, it worked!

(I''d tried forward declaration, but my compiler whined about it; I just needed to move #include "myinc" to the cpp file instead.)

-=skjin=-

Share this post


Link to post
Share on other sites