>>I've got 2 classes in 2 seperate header files, but within the same
>>namespace. If I use a line like:
[quoted text clipped - 20 lines]
>
> -cd
Thanks for the response!
What if each class references the other one (circular references). How
would you handle that?
Carl Daniel [VC++ MVP] - 10 Mar 2005 22:47 GMT
>>> I've got 2 classes in 2 seperate header files, but within the same
>>> namespace. If I use a line like:
[quoted text clipped - 25 lines]
> What if each class references the other one (circular references). How
> would you handle that?
class X;
class Y
{
X* m_X;
};
class X
{
Y* m_y;
};
If you need member functions of the classes to access members of the "other
type", you'll need to define your class implementation outside the class
definition:
struct X
{
int m_i;
void useY();
};
struct Y
{
int m_j;
void useX();
};
void X::useY()
{
Y y;
y.m_j;
}
void Y::useX()
{
X x;
x.m_i;
}
A "forward declaration" (or "incomplete class declaration" in standardese)
allows you to refer to a class in contexts that don't require knowing the
size or interface of a class (such as declaring a pointer or reference
variable or parameter). In order to declare a variable of the class type,
or access any members of the class, a complete class is required.
Between forward declarations and out of line member definitions, you should
be able to handle any sensible mutual dependency.
-cd
Bob Milton - 10 Mar 2005 22:49 GMT
Stephen,
Say you have classes X and Y that reference each other. The two headers
would look something like:
X.H:
class Y ;
class X
{
.
.
.
Y *myYRef ;
.
.
} ;
and Y.H would look something like:
class X ;
class Y
{
.
.
.
X *myXRef ;
.
} ;
Then the implementations of X and Y need to include both header files. (And
users of either may also need both headers).
Bob Milton
>>>I've got 2 classes in 2 seperate header files, but within the same
>>>namespace. If I use a line like:
[quoted text clipped - 25 lines]
> What if each class references the other one (circular references). How
> would you handle that?