A REALLY easy classes question

Started by
3 comments, last by benjamin bunny 23 years, 9 months ago
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

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

Advertisement

Prototypes!
    class a;class b;class a{b* lpb;};class b{};    
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
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

~deadlinegrunt

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
Dark Lord Pi
I did wonder if you could do that, but when I tried it last it crashed, so I assumed you couldn''t. Probably a problem somewhere else in my code. Anyway, thanks for the responses.

http://www.geocities.com/ben32768

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

This topic is closed to new replies.

Advertisement