Nested class definitions

Discussion in 'C++' started by Tony Johansson, May 19, 2005.

  1. Hello!

    What is the difference if we have the whole class Item defined within class
    List see NOTE_1 below compare to
    having class Item defined stand-alone and only have an instance of class
    Item in the private section like NOTE_2.
    Is it any advantage to nest a class definition within another class
    definition such as this List class below.

    Many thanks!

    //Tony

    NOTE_2
    ********
    class List
    {
    public
    ....
    private:
    Item obj;
    };

    Assume we have this nested class definition

    NOTE_1
    *******
    class List
    {
    public
    ....
    private:
    class Item
    {
    public:
    item();
    };
    };
    Tony Johansson, May 19, 2005
    #1
    1. Advertising

  2. Tony Johansson wrote:
    > What is the difference if we have the whole class Item defined within
    > class List see NOTE_1 below compare to
    > having class Item defined stand-alone and only have an instance of
    > class Item in the private section like NOTE_2.


    The former case (NOTE_1) does not have an _instance_ of 'item' in
    a 'List' object.

    > Is it any advantage to nest a class definition within another class
    > definition such as this List class below.


    It only limits the scope of the nested class.

    Unlike Java, C++ does not automatically create an instance of the
    nested class in the outer class.

    > Many thanks!
    >
    > //Tony
    >
    > NOTE_2
    > ********
    > class List
    > {
    > public
    > ...
    > private:
    > Item obj;
    > };
    >
    > Assume we have this nested class definition
    >
    > NOTE_1
    > *******
    > class List
    > {
    > public
    > ...
    > private:
    > class Item
    > {
    > public:
    > item();
    > };
    > };


    V
    Victor Bazarov, May 19, 2005
    #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. E11
    Replies:
    1
    Views:
    4,736
    Thomas Weidenfeller
    Oct 12, 2005
  2. Paddy

    Nested Parameter Definitions

    Paddy, Feb 25, 2007, in forum: Python
    Replies:
    10
    Views:
    463
    Paddy
    Feb 26, 2007
  3. request@no_spam.com
    Replies:
    5
    Views:
    420
  4. Jonah Bloch-Johnson

    Nested Arrays in Method Definitions--a puzzle

    Jonah Bloch-Johnson, Aug 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    110
    Jonah Bloch-Johnson
    Aug 20, 2008
  5. Jon
    Replies:
    0
    Views:
    266
Loading...

Share This Page