• Advertisement
Sign in to follow this  

Stupid class problem

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

Hi there! I'm having a little problem bothering quite some time ;-) Look at this: class ClassA { public: ClassB myB; }; class ClassB { public: ClassA myA; }; This code won't work because ClassB isn't defined when ClassA gets compiled... is there a way to ceate somekind of "prototypes" for classes? Thank's a lot!

Share this post


Link to post
Share on other sites
Advertisement
Assuming this is C++, you can't recursively store variables by value like that; it would require infinite size for each of the classes. You can store pointers, and if you use pointers you can create forward declarations for the classes. ex:

class ClassB;

class ClassA {
public:
ClassB * myB;
};

Share this post


Link to post
Share on other sites

class ClassB;
class ClassA
{
public:
ClassB *myB;
};

class ClassB
{
public:
ClassA myA;
};

You will have to use pointers, but it will work like this.

Tom

EDIT: D'oh :)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by dimebolt
You will have to use pointers, but it will work like this.


Or, as a better option, smart pointers.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement