Exercise: how to remove mutual dependencies ?

Discussion in 'C++' started by Anonimo, Mar 13, 2011.

  1. Anonimo

    Anonimo Guest

    Hello,

    I have two classes with one static member in each class. Each static
    member uses/references the other static member during construction, and
    viceversa. Is there a way to remove the mutual dependencies by modifying
    the classes containing the static members?

    Can you provide a solution useful for a real case with minimal
    definitions of both the classes and the static members?

    Thanks.
    Anonimo, Mar 13, 2011
    #1
    1. Advertising

  2. Anonimo

    Anonimo Guest

    > It sounds like you have two static objects defined in different TUs
    > which reference each other; this is bad as the construction order of
    > static objects across multiple TUs is undefined. You need to rethink
    > your design keeping static objects to a minimum; static objects are more
    > or less just as bad as global variables.


    This exercise has been taken from a Stroustrup's book. The exercise does
    not impose to define the members in two different TUs, but explicitly
    asks how to avoid the mutual dependency by modifying the containing
    classes.. The exercise also asks to mention a possible real case where
    one static member could use the other and viceversa.

    I know static objects might be bad is some cases, but this is an
    exercise and I must answer to what it asks for.

    Thanks
    Anonimo, Mar 13, 2011
    #2
    1. Advertising

  3. Anonimo

    Öö Tiib Guest

    On Mar 13, 4:44 pm, Anonimo <> wrote:
    > Hello,
    >
    > I have two classes with one static member in each class. Each static
    > member uses/references the other static member during construction, and
    > viceversa. Is there a way to remove the mutual dependencies by modifying
    > the classes containing the static members?
    >
    > Can you provide a solution useful for a real case with minimal
    > definitions of both the classes and the static members?


    There are no real cases where you need such monsters. So ... minimal
    solution of real case is to delete both the static members.
    Öö Tiib, Mar 13, 2011
    #3
  4. Anonimo <> wrote:
    > I have two classes with one static member in each class. Each static
    > member uses/references the other static member during construction, and
    > viceversa. Is there a way to remove the mutual dependencies by modifying
    > the classes containing the static members?


    // Header:
    class MyClass
    {
    private:
    class InnerClass;
    static InnerClass& innerClass();
    //...
    };

    // Compilation unit:
    class MyClass::InnerClass
    {
    //...
    };

    MyClass::InnerClass& MyClass::innerClass()
    {
    static InnerClass instance;
    return instance;
    }
    Juha Nieminen, Mar 13, 2011
    #4
  5. Anonimo

    Öö Tiib Guest

    On Mar 13, 11:10 pm, Juha Nieminen <> wrote:
    > Anonimo <> wrote:
    > > I have two classes with one static member in each class. Each static
    > > member uses/references the other static member during construction, and
    > > viceversa. Is there a way to remove the mutual dependencies by modifying
    > > the classes containing the static members?

    >
    > // Header:
    > class MyClass
    > {
    >  private:
    >     class InnerClass;
    >     static InnerClass& innerClass();
    >     //...
    >
    > };
    >
    > // Compilation unit:
    > class MyClass::InnerClass
    > {
    >     //...
    >
    > };
    >
    > MyClass::InnerClass& MyClass::innerClass()
    > {
    >     static InnerClass instance;
    >     return instance;
    >
    >
    >
    > }


    It is private static so you can remove any traces of it from interface
    of class to implementation.
    Öö Tiib, Mar 13, 2011
    #5
  6. Öö Tiib <> wrote:
    > It is private static so you can remove any traces of it from interface
    > of class to implementation.


    You have to declare the inner class inside the outer class if you
    want the inner class to be able to access the private section of the
    outer class.
    Juha Nieminen, Mar 15, 2011
    #6
  7. Anonimo

    Öö Tiib Guest

    On Mar 15, 4:30 pm, Juha Nieminen <> wrote:
    > Öö Tiib <> wrote:
    > > It is private static so you can remove any traces of it from interface
    > > of class to implementation.

    >
    >   You have to declare the inner class inside the outer class if you
    > want the inner class to be able to access the private section of the
    > outer class.


    Ok. I have never needed private static members for such access. So ...
    i have no experience. If i did need such access then perhaps a
    (forward declared) friend or private class would grant same?
    Öö Tiib, Mar 15, 2011
    #7
  8. Öö Tiib <> wrote:
    > On Mar 15, 4:30 pm, Juha Nieminen <> wrote:
    >> Öö Tiib <> wrote:
    >> > It is private static so you can remove any traces of it from interface
    >> > of class to implementation.

    >>
    >>   You have to declare the inner class inside the outer class if you
    >> want the inner class to be able to access the private section of the
    >> outer class.

    >
    > Ok. I have never needed private static members for such access. So ...
    > i have no experience. If i did need such access then perhaps a
    > (forward declared) friend or private class would grant same?


    Isn't that exactly what I did?
    Juha Nieminen, Mar 16, 2011
    #8
  9. Anonimo

    Öö Tiib Guest

    On Mar 16, 8:01 pm, Juha Nieminen <> wrote:
    > Öö Tiib <> wrote:
    > > On Mar 15, 4:30 pm, Juha Nieminen <> wrote:
    > >> Öö Tiib <> wrote:
    > >> > It is private static so you can remove any traces of it from interface
    > >> > of class to implementation.

    >
    > >>   You have to declare the inner class inside the outer class if you
    > >> want the inner class to be able to access the private section of the
    > >> outer class.

    >
    > > Ok. I have never needed private static members for such access. So ...
    > > i have no experience. If i did need such access then perhaps a
    > > (forward declared) friend or private class would grant same?

    >
    >   Isn't that exactly what I did?


    Yes. I was about private static that i have never needed to expose in
    header ... neither functions nor data members.
    Öö Tiib, Mar 16, 2011
    #9
    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:
    441
    Michael Borgwardt
    Oct 17, 2003
  2. Giambattista Bloisi

    non-blocking mutual exclusion

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

    Re: Mutual subclass dependency

    Bob Hairgrove, Oct 19, 2004, in forum: C++
    Replies:
    1
    Views:
    383
    Martin Magnusson
    Oct 19, 2004
  4. mosfet

    mutual class dependencies

    mosfet, Jan 24, 2008, in forum: C++
    Replies:
    19
    Views:
    513
    Pavel
    Jan 29, 2008
  5. Csaba  Gabor

    Remove trailing comments exercise

    Csaba Gabor, Nov 4, 2009, in forum: Javascript
    Replies:
    24
    Views:
    294
    Lasse Reichstein Nielsen
    Nov 8, 2009
Loading...

Share This Page