forward declaration?

L

Lem

I have the following 2 classes:
class A{
B * b;
....
};
class B{
A * a;
....
};
The problem is I cannot declare class A without declaring class B first, and
vice versa, as each declares a variable of the other class. How do I solve
this problem?

Thanks in advance,
Lem
 
M

Michael Demanet

Lem said:
I have the following 2 classes:
class A{
B * b;
...
};
class B{
A * a;
...
};
The problem is I cannot declare class A without declaring class B first, and
vice versa, as each declares a variable of the other class. How do I solve
this problem?

Thanks in advance,
Lem

why not using forward class definition

try to append this in the header file
class A;
class B;
before the declaration of the classes.
 
R

Rob Williscroft

Lem wrote in
I have the following 2 classes:

class B;

The above declares B as a class-type, its an incomplete declaration so
you can't do say: class C { B b; }; but you can declare pointers (as
you want to below) refrences. The key is that you can't do anything
with A that requires a *complete* (has a body {}) defenition of A,
some examples bellow.
class A{
B * b;
...
};
class B{
A * a;
...
};
The problem is I cannot declare class A without declaring class B
first, and vice versa, as each declares a variable of the other class.
How do I solve this problem?

<example>
#include <iostream>
#include <ostream>

class A;

class C
{
public:
A *pointer;
A &reference;
static A static_a;
C( A &r ) : pointer( 0 ), reference( r ) {}
};

/* without extern this would require a complete declaration
*/
extern A extern_a;

/* This is only a declaration so its ok, we can't call it or
give a *defenition* intill A is *complete*
*/
void function( A a );

class A
{
public:
int a;
};

/* A is complete so we can now *define* things that need a
*complete* defenition of A
*/
A C::static_a = { 1 };
A extern_a = { 2 };

void function( A a )
{
std::cout << a.a << std::endl;
}

int main()
{
A a;
C c(a);
c.pointer = &a;

a.a = 3;

function( a );
function( c.reference );

function( C::static_a );

function( extern_a );

}
</example>

Rob.
 
O

osmium

Lem said:
I have the following 2 classes:
class A{
B * b;
...
};
class B{
A * a;
...
};
The problem is I cannot declare class A without declaring class B first, and
vice versa, as each declares a variable of the other class. How do I solve
this problem?

Thanks in advance,
Lem
 
F

Fraser Ross

"osmium"
You can't solve what you have shown. You have infinite recursion, or
something (I am not a linguist). An A contains a B and a B contains an A,
and so on to infinity. There is not enough context there so we can deduce
what you are trying to do. Just kind of flailing at what you want; do a
forward declaration of B and use pointers or references. If that doesn't
get what you want, explain what you want to accomplish in more detail.
A contains a pointer to a B. Pointers have a known size and hence the code
only needs to be told that B is a class:
class A{
class B * b;
...
};

Class B is accepted because A has been seen and its size is known.

Fraser.
 
O

osmium

Lem said:
I have the following 2 classes:
class A{
B * b;
...
};
class B{
A * a;
...
};
The problem is I cannot declare class A without declaring class B first, and
vice versa, as each declares a variable of the other class. How do I solve
this problem?

You can't solve what you have shown. You have infinite recursion, or
something (I am not a linguist). An A contains a B and a B contains an A,
and so on to infinity. There is not enough context there so we can deduce
what you are trying to do. Just kind of flailing at what you want; do a
forward declaration of B and use pointers or references. If that doesn't
get what you want, explain what you want to accomplish in more detail.
 
L

lallous

Hello,

How to solve then this forward problem:

class A;
class B
{
public:
A *a;
void test()
{
int x = A::A1;
}
};

class A
{
public:
enum
{
A1,
A2
};
};


I need to access A's enum from class B.

Regards,
Elias
 
J

John Carson

lallous said:
Hello,

How to solve then this forward problem:

class A;
class B
{
public:
A *a;
void test()
{
int x = A::A1;
}
};

class A
{
public:
enum
{
A1,
A2
};
};


I need to access A's enum from class B.

Regards,
Elias


Only declare test() in the class declaration. Define it later.


class A;
class B
{
public:
A *a;
void test();
};

class A
{
public:
enum
{
A1,
A2
};
};


inline void B::test()
{
int x = A::A1;
}
 
O

osmium

Fraser said:
"osmium"
A contains a pointer to a B. Pointers have a known size and hence the code
only needs to be told that B is a class:
class A{
class B * b;
...
};

Class B is accepted because A has been seen and its size is known.

Oops! Sorry about that, I am so used to seeing what I thought I saw that I
saw it yet again.
 
J

jeffc

osmium said:
You can't solve what you have shown. You have infinite recursion, or
something (I am not a linguist). An A contains a B and a B contains an
A...

No, not really. A contains a ptr to a B, which might be zero, or it might
point to a B which has a 0 ptr to an A, etc.
 
J

jeffc

lallous said:
Hello,

How to solve then this forward problem:

class A;
class B
{
public:
A *a;
void test()
{
int x = A::A1;
}
};

class A
{
public:
enum
{
A1,
A2
};
};


I need to access A's enum from class B.

There is no similar problem here. Just define A before B. A has no
reference to B, so obviously it needs to come first.
 

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

Forum statistics

Threads
473,767
Messages
2,569,573
Members
45,046
Latest member
Gavizuho

Latest Threads

Top