Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Cyclic dependencies

Reply
Thread Tools

Cyclic dependencies

 
 
xiaohuamao
Guest
Posts: n/a
 
      08-09-2006
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;
}

 
Reply With Quote
 
 
 
 
Pierre Barbier de Reuille
Guest
Posts: n/a
 
      08-09-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Smart pointer and cyclic dependencies Marcel Müller C++ 1 03-22-2008 10:39 AM
Functional issues of cyclic dependencies Jack Java 6 02-09-2008 08:24 PM
Forward Declaration produces an error (no cyclic dependencies tho) elmar_macek@gmx.de C++ 2 09-26-2007 12:50 PM
Cyclic dependencies and namespaces. Craig Sanders C++ 1 09-05-2007 04:51 PM
templates and cyclic dependencies Leslaw Bieniasz C++ 3 09-22-2004 02:08 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57