mutual dependency

Discussion in 'C++' started by thomas, Oct 30, 2009.

  1. thomas

    thomas Guest

    -------------code------------
    class A{
    public:
    A(){}
    void f(){
    B *b = new B();
    }
    };

    class B{
    public:
    B(){}
    void f(){
    A *a = new A();
    }
    };
    ---------------code------------
    for the above sample code, there's compile error.
    if I put a declaration "class B;" at the begining, it says that no
    default constructor.
    how to declare a default constructor to avoid the compile error?
     
    thomas, Oct 30, 2009
    #1
    1. Advertising

  2. On Oct 30, 11:47 am, thomas <> wrote:
    > -------------code------------
    > class A{
    > public:
    >      A(){}
    >      void f(){
    >            B *b = new B();
    >      }
    >
    > };
    >
    > class B{
    > public:
    >       B(){}
    >       void f(){
    >           A *a = new A();
    >       }};
    >
    > ---------------code------------
    > for the above sample code, there's compile error.
    > if I put a declaration "class B;" at the begining, it says that no
    > default constructor.
    > how to declare a default constructor to avoid the compile error?


    Hi Thomas

    Put the definition of A::f() after the the defintion of B:

    class A{
    public:
    A(){}
    void f();
    };


    class B{
    public:
    B(){}
    void f() {
    A *a = new A();
    }

    };

    void A::f()
    {
    B *b = new class B();
    }

    it compiles and works.

    FYI, such mutual dependency isn't good sign of object-oriented design.

    Regards,
    -- Saeed Amrollahi
     
    Saeed Amrollahi, Oct 30, 2009
    #2
    1. Advertising

  3. Saeed Amrollahi wrote:
    > On Oct 30, 11:47 am, thomas <> wrote:
    >> -------------code------------
    >> class A{
    >> public:
    >> A(){}
    >> void f(){
    >> B *b = new B();
    >> }
    >>
    >> };
    >>
    >> class B{
    >> public:
    >> B(){}
    >> void f(){
    >> A *a = new A();
    >> }};
    >>
    >> ---------------code------------
    >> for the above sample code, there's compile error.
    >> if I put a declaration "class B;" at the begining, it says that no
    >> default constructor.
    >> how to declare a default constructor to avoid the compile error?

    >
    > [..]
    >
    > FYI, such mutual dependency isn't good sign of object-oriented design.


    Really? Why is that?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Oct 30, 2009
    #3
  4. On Oct 30, 3:21 pm, Victor Bazarov <> wrote:
    > Saeed Amrollahi wrote:
    > > On Oct 30, 11:47 am, thomas <> wrote:
    > >> -------------code------------
    > >> class A{
    > >> public:
    > >>      A(){}
    > >>      void f(){
    > >>            B *b = new B();
    > >>      }

    >
    > >> };

    >
    > >> class B{
    > >> public:
    > >>       B(){}
    > >>       void f(){
    > >>           A *a = new A();
    > >>       }};

    >
    > >> ---------------code------------
    > >> for the above sample code, there's compile error.
    > >> if I put a declaration "class B;" at the begining, it says that no
    > >> default constructor.
    > >> how to declare a default constructor to avoid the compile error?

    >
    > > [..]

    >
    > > FYI, such mutual dependency isn't good sign of object-oriented design.

    >
    > Really?  Why is that?
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask- Hide quoted text -
    >
    > - Show quoted text -


    Hi Victor
    I just mean, with a good design, we can avoid such mutual
    dependencies.
    I face with the following code patterns a lot of times:
    class A {
    B* pB;
    };

    class B {
    A* pA;
    };

    with a review on class design I can avoid such "mutual" dependencies.

    Regards,
    -- Saeed Amrollahi
     
    Saeed Amrollahi, Oct 30, 2009
    #4
  5. Stuart Golodetz wrote:
    > Victor Bazarov wrote:
    >> Saeed Amrollahi wrote:
    >>> On Oct 30, 11:47 am, thomas <> wrote:
    >>>> -------------code------------
    >>>> class A{
    >>>> public:
    >>>> A(){}
    >>>> void f(){
    >>>> B *b = new B();
    >>>> }
    >>>>
    >>>> };
    >>>>
    >>>> class B{
    >>>> public:
    >>>> B(){}
    >>>> void f(){
    >>>> A *a = new A();
    >>>> }};
    >>>>
    >>>> ---------------code------------
    >>>> for the above sample code, there's compile error.
    >>>> if I put a declaration "class B;" at the begining, it says that no
    >>>> default constructor.
    >>>> how to declare a default constructor to avoid the compile error?
    >>>
    >>> [..]
    >>>
    >>> FYI, such mutual dependency isn't good sign of object-oriented design.

    >>
    >> Really? Why is that?
    >>
    >> V

    >
    > I suspect you're playing devil's advocate here Victor :)


    Saeed in his reply quoted a different kind of dependency than the OP
    had, and that's one of my points - there are different types of
    dependency, and this particular one usually isn't especially bad, or an
    indication of a bad design. For example, updating data usually follows
    with updating the view associated with data, and vice versa, user
    interaction with the view can cause an update in the data... Is that a
    flaw in the design? Mmm... No.

    > I'm going to
    > direct the OP to Lakos's Large-Scale C++ Software Design, which had
    > quite a good discussion of issues to do with cyclic dependencies. (I
    > know it's possibly a bit dated in some ways now, but I remember it being
    > fairly decent.)


    It's a decent book once you learn to skip irrelevant or obsolete parts.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Oct 30, 2009
    #5
  6. Saeed Amrollahi wrote:
    > On Oct 30, 3:21 pm, Victor Bazarov <> wrote:
    >> Saeed Amrollahi wrote:
    >>> On Oct 30, 11:47 am, thomas <> wrote:
    >>>> -------------code------------
    >>>> class A{
    >>>> public:
    >>>> A(){}
    >>>> void f(){
    >>>> B *b = new B();
    >>>> }
    >>>> };
    >>>> class B{
    >>>> public:
    >>>> B(){}
    >>>> void f(){
    >>>> A *a = new A();
    >>>> }};
    >>>> ---------------code------------
    >>>> for the above sample code, there's compile error.
    >>>> if I put a declaration "class B;" at the begining, it says that no
    >>>> default constructor.
    >>>> how to declare a default constructor to avoid the compile error?
    >>> [..]
    >>> FYI, such mutual dependency isn't good sign of object-oriented design.

    >> Really? Why is that?
    >>
    >> V
    >> --
    >> Please remove capital 'A's when replying by e-mail
    >> I do not respond to top-posted replies, please don't ask- Hide quoted text -
    >>
    >> - Show quoted text -

    >
    > Hi Victor
    > I just mean, with a good design, we can avoid such mutual
    > dependencies.


    Do explain. Please.

    > I face with the following code patterns a lot of times:
    > class A {
    > B* pB;
    > };
    >
    > class B {
    > A* pA;
    > };
    >
    > with a review on class design I can avoid such "mutual" dependencies.


    But that's not what the OP has. Look again.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Oct 30, 2009
    #6
    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. Vjeran Marcinko

    Mutual EJB possible?

    Vjeran Marcinko, Oct 17, 2003, in forum: Java
    Replies:
    3
    Views:
    454
    Michael Borgwardt
    Oct 17, 2003
  2. Giambattista Bloisi

    non-blocking mutual exclusion

    Giambattista Bloisi, Dec 16, 2003, in forum: Java
    Replies:
    1
    Views:
    413
    Matt Humphrey
    Dec 16, 2003
  3. Bob Hairgrove

    Re: Mutual subclass dependency

    Bob Hairgrove, Oct 19, 2004, in forum: C++
    Replies:
    1
    Views:
    393
    Martin Magnusson
    Oct 19, 2004
  4. .
    Replies:
    0
    Views:
    593
  5. Replies:
    2
    Views:
    259
Loading...

Share This Page