Nested class definitions

T

Tony Johansson

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();
};
};
 
V

Victor Bazarov

Tony said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top