Nested classes

Discussion in 'C++' started by Matthias =?ISO-8859-1?Q?K=E4ppler?=, Nov 23, 2004.

  1. Hello,

    I just wanted to ask, if an outer and a nested class share implementation
    details, e.g. in that both need to have access to a member of the nested
    class, is it common practice to simply make those members of the nested
    class public?

    class Outer
    {
    class Inner
    {
    int bar; // Outer needs access to bar
    };
    };

    Should I just make 'bar' public? Or is there a better solution to access
    data in an inner class? I think in Java there are mechanisms built in the
    language to exchange data between an inner and outer class, something like
    a second this pointer which points to the inner class (or the other way
    around, I cant quite remember).

    Thanks in advance,
    Matthias
     
    Matthias =?ISO-8859-1?Q?K=E4ppler?=, Nov 23, 2004
    #1
    1. Advertising

  2. > I just wanted to ask, if an outer and a nested class share implementation
    > details, e.g. in that both need to have access to a member of the nested
    > class, is it common practice to simply make those members of the nested
    > class public?
    >
    > class Outer
    > {
    > class Inner
    > {
    > int bar; // Outer needs access to bar
    > };
    > };
    >


    What's the use of an inner class if the outer needs what's inside the inner?


    By making bar public, users can do

    int main()
    {
    // if Inner is public
    Outer::Inner k;
    k.bar = 10;
    }

    or

    Outer::Outer()
    {
    Inner k;
    k.bar = 10;
    }

    which is not necessarily a good thing, whether Inner is public or not.
    Just use some member functions to fiddle with the member variables.

    Outer::Outer()
    {
    Inner k;
    k.set_bar(10);
    }



    Jonathan
     
    Jonathan Mcdougall, Nov 23, 2004
    #2
    1. Advertising

  3. Jonathan Mcdougall wrote:

    > What's the use of an inner class if the outer needs what's inside the
    > inner?


    The inner class is a whole own thing. It's derived from a totally different
    base class for example. Yet they need both access to a std::list.

    > By making bar public, users can do


    No, my inner class is declared in the private section of Outer of course.
    Like in the example. The nested class is not even visible to the public,
    leave alone accessible. It's an implementation detail of Outer. Only Outer
    has to access it.

    > Just use some member functions to fiddle with the member variables.
    >
    > Outer::Outer()
    > {
    > Inner k;
    > k.set_bar(10);
    > }


    Outer needs to access data inside Inner. What's am I gaining with declaring
    a getter in the nested class? Then I can as well use public members.

    - Matthias
     
    Matthias =?ISO-8859-1?Q?K=E4ppler?=, Nov 23, 2004
    #3
  4. >>Just use some member functions to fiddle with the member variables.
    >>
    >>Outer::Outer()
    >>{
    >> Inner k;
    >> k.set_bar(10);
    >>}

    >
    >
    > Outer needs to access data inside Inner. What's am I gaining with declaring
    > a getter in the nested class?


    Access control.

    > Then I can as well use public members.


    Of course.

    If I understand the problem correctly, the inner class has some data the
    outer class has to access. Making that data part of the global
    namespace, in the outer or inner class depends on your design, which you
    said nothing about. If the outer class needs the inner's data, either
    make it public of use accessors.

    Just remember that you'll always need a real object to get the data,
    there is no this-like keywords for inner or outer classes.


    Jonathan
     
    Jonathan Mcdougall, Nov 23, 2004
    #4
  5. Jonathan Mcdougall wrote:

    >>>Just use some member functions to fiddle with the member variables.
    >>>
    >>>Outer::Outer()
    >>>{
    >>> Inner k;
    >>> k.set_bar(10);
    >>>}

    >>
    >>
    >> Outer needs to access data inside Inner. What's am I gaining with
    >> declaring a getter in the nested class?

    >
    > Access control.
    >
    > > Then I can as well use public members.

    >
    > Of course.
    >
    > If I understand the problem correctly, the inner class has some data the
    > outer class has to access. Making that data part of the global
    > namespace, in the outer or inner class depends on your design, which you
    > said nothing about. If the outer class needs the inner's data, either
    > make it public of use accessors.
    >
    > Just remember that you'll always need a real object to get the data,
    > there is no this-like keywords for inner or outer classes.
    >
    >
    > Jonathan


    Okay, thanks Jonathan.
     
    Matthias =?ISO-8859-1?Q?K=E4ppler?=, Nov 23, 2004
    #5
    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. Razvan
    Replies:
    5
    Views:
    11,310
    Dale King
    Jul 27, 2004
  2. kelvSYC
    Replies:
    2
    Views:
    594
    Chris Smith
    Aug 18, 2004
  3. Russ Perry Jr
    Replies:
    2
    Views:
    4,145
    Russ Perry Jr
    Aug 20, 2004
  4. Tony Johansson

    Nested classes within other classes

    Tony Johansson, Dec 14, 2004, in forum: C++
    Replies:
    2
    Views:
    483
    msalters
    Dec 14, 2004
  5. Chad E. Dollins
    Replies:
    3
    Views:
    659
    Kai-Uwe Bux
    Nov 8, 2005
Loading...

Share This Page