• Advertisement

Archived

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

A REALLY easy classes question

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

I have 2 classes - A and B, and class A has a pointer to an object of class B, and vice versa. However, when I try and compile, obviously the compiler doesn''t recognise the class B pointer in class A as class B hasn''t yet been defined. The question is, how do I do this? Any help would be much appreciated. Here''s the source
    
class A
{
  B * Bp;
  
};

class B
{
  A * Ap;
};
    
http://www.geocities.com/ben32768

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by benjamin bunny

I have 2 classes - A and B, and class A has a pointer to an object of class B, and vice versa. However, when I try and compile, obviously the compiler doesn''t recognise the class B pointer in class A as class B hasn''t yet been defined. The question is, how do I do this? Any help would be much appreciated.

Here''s the source

    

//

// Forward reference here.

//

class B;

class A
{
B * Bp;

};

class B
{
A * Ap;
};





http://www.geocities.com/ben32768




YAP YFIO,
deadlinegrunt

Share this post


Link to post
Share on other sites
Use class prototypes. You can do this because the second class isn''t used in class A''s declaration.


--:
class B;

class A
{
B * Bp;
};

class B
{
A * Ap;
};



--:
Note: if class A were declared as:
class A
{
B aB;
};

it wouldn''t work, since B hasn''t been declared, so the compiler won''t know the size of B, and will be unable to allocate space for the B member of class A.


Dark Lord Pi

Share this post


Link to post
Share on other sites

  • Advertisement