xiaohuamao wrote:
> Beside declare independent abstract classes, is there any other methods
> to solve the problem of cyclic dependencies?Like the following one,
> which causes compile error of undefined type.
>
> class Child;
>
> class Parent
> {
> public:
> int a;
> Child *myChild;
> bool test() {return myChild->b == 0;}
> };
>
> class Child
> {
> public:
> int b;
> Parent *myParent;
> bool test() {return myParent->a == 0; }
> };
>
> int main()
> {
> return 0;
> }
>
Very simple, just put the bits that needs both definitions *after* both
definitions actually occured.
class Child;
class Parent
{
public:
int a;
Child* myChild;
bool test();
};
class Child
{
// Same as yours
};
bool Parent::test() { return myChild->b == 0; }
Pierre
|