Mutually referential classes

C

Colin JN Breame

Say there are two classes:

class A {
B *b;
};

class B {
A *a;
};

How can this be defined and compiled?

Thanks
 
T

The Flib

Colin JN Breame said:
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;
}
 
C

c++novice

Colin JN Breame said:
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;
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top