Archived

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

cyberg

class to class to class

Recommended Posts

cyberg    122
hello, i have this case: class class1{ class2 cls2; }; class class2{ class1 cls1; }; i dont know how to make the class1 see the class2 and vice versa at the same time, anyone know how to do that? thx, cyberg

Share this post


Link to post
Share on other sites
karmalaa    122
All you have to do is to use a FORWARD DECLARATION of the second class. That is, your DECLARE that the class exists but you''ll DEFINE it somewhere else.

  

// Forward declaration.

class C2ndClass;

// Define the 1st class...

class C1stClass {
C2ndClass m_2ndClass;
};

// ... and the 2nd too...

class C2stClass {
C1stClass m_1stClass;
};



Hope it helps...


Karmalaa






[home page] [e-mail]

---
"Lifting shadows off a dream once broken
She can turn a drop of water into an ocean"

Share this post


Link to post
Share on other sites
FallingFrog    122
dude... I think what you want is for each class to have a *pointer* to the other. If a class1 object has a class2, and that has a class1, and that has a class2... It''s a recursive definition. That why you''re not allowed to say

class InfiniteClass
{
public:

InfiniteClass Infinity;
};

You see what I mean? But it''s legal to say

class OKClass
{
public:

OKClass *GoodEnough;
};

Hope I helped...

Share this post


Link to post
Share on other sites