Cyclic dependencies

Discussion in 'C++' started by xiaohuamao, Aug 9, 2006.

  1. xiaohuamao

    xiaohuamao Guest

    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;
    }
    xiaohuamao, Aug 9, 2006
    #1
    1. Advertising

  2. 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
    Pierre Barbier de Reuille, Aug 9, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leslaw Bieniasz

    templates and cyclic dependencies

    Leslaw Bieniasz, Sep 20, 2004, in forum: C++
    Replies:
    3
    Views:
    401
    Victor Bazarov
    Sep 22, 2004
  2. Craig Sanders

    Cyclic dependencies and namespaces.

    Craig Sanders, Sep 5, 2007, in forum: C++
    Replies:
    1
    Views:
    401
    Glen Dayton
    Sep 5, 2007
  3. Replies:
    2
    Views:
    475
  4. Jack
    Replies:
    6
    Views:
    346
    Patricia Shanahan
    Feb 9, 2008
  5. Marcel Müller

    Smart pointer and cyclic dependencies

    Marcel Müller, Mar 21, 2008, in forum: C++
    Replies:
    1
    Views:
    623
    Marcel Müller
    Mar 22, 2008
Loading...

Share This Page