Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Mutually referential classes

Reply
Thread Tools

Mutually referential classes

 
 
Colin JN Breame
Guest
Posts: n/a
 
      01-21-2004
Say there are two classes:

class A {
B *b;
};

class B {
A *a;
};

How can this be defined and compiled?

Thanks
 
Reply With Quote
 
 
 
 
Sumit Rajan
Guest
Posts: n/a
 
      01-21-2004

"Colin JN Breame" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> Say there are two classes:



class B;

> class A {
> B *b;
> };
>
> class B {
> A *a;
> };
>
> How can this be defined and compiled?
>
> Thanks



 
Reply With Quote
 
 
 
 
The Flib
Guest
Posts: n/a
 
      01-21-2004
Colin JN Breame <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Say there are two classes:
>
> class A {
> B *b;
> };
>
> class B {
> A *a;
> };
>
> How can this be defined and compiled?
>
> Thanks


Heya. What you need to do is stick a forward declaration of B before
you define A. The compiler is way smarter than we mere humans, so it
figures out what you mean when you say:

class B;

class A
{
B *b;
};

class B
{
A *a;
}
 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      01-21-2004
On Thu, 22 Jan 2004 01:42:00 +0530 in comp.lang.c++, "Sumit Rajan"
<(E-Mail Removed)> was alleged to have written:
>> Say there are two classes:


This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[38.11] How can I create two classes that both know about each other?"
It is always good to check the FAQ before posting. You can get the FAQ
at:
http://www.parashift.com/c++-faq-lite/


 
Reply With Quote
 
c++novice
Guest
Posts: n/a
 
      01-22-2004
Colin JN Breame <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Say there are two classes:
>
> class A {
> B *b;
> };
>
> class B {
> A *a;
> };
>
> How can this be defined and compiled?
>
> Thanks


Do a forward declaration--

Class B; //declared

class A {
B *b;
};

class B { //defined
A *a;
};
 
Reply With Quote
 
Colin JN Breame
Guest
Posts: n/a
 
      01-22-2004
On Wed, 21 Jan 2004 23:53:02 +0000, David Harmon wrote:

> On Thu, 22 Jan 2004 01:42:00 +0530 in comp.lang.c++, "Sumit Rajan"
> <(E-Mail Removed)> was alleged to have written:
>>> Say there are two classes:

>
> This issue is covered in Marshall Cline's C++ FAQ. See the topic "[38.11]
> How can I create two classes that both know about each other?" It is
> always good to check the FAQ before posting. You can get the FAQ at:
> http://www.parashift.com/c++-faq-lite/


Oh yes, should have checked.

Thanks anyway!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mutually referential class templates jrwats C++ 6 01-18-2009 08:36 PM
mutually referential (Pg 140 K&R2) G Patel C Programming 14 05-02-2005 07:24 PM
How to handle mutually dependent classes Brad Kartchner C++ 10 05-01-2004 06:43 PM
Re: Mutually referential types Eric C Programming 5 08-17-2003 12:45 AM
Re: Mutually referential types Chris Torek C Programming 1 08-16-2003 04:11 PM



Advertisments