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.