Nested classes

V

Vinodh Kumar P

I abstract a Car, and the parts wihtin a car, with a C++ class.
Since only my version of class Car uses the classes defined for its parts,
is it a good practice to do like this?

class Car
{
class Wheel
{
 
J

John Harrison

Vinodh Kumar P said:
I abstract a Car, and the parts wihtin a car, with a C++ class.
Since only my version of class Car uses the classes defined for its parts,
is it a good practice to do like this?

class Car
{
class Wheel
{
.
.
.
};

class Engine
{
.
.
.
};

class Body
{
.
.
.
};

class FuelTank
{
.
.
.
};

AndAHostofMoreInnerClasses
{
};

};


Vinodh

I don't think so. If you've designed Wheel and FuelTank well its quite
possible that some else will want to use them in the future even if they are
not being used now, by making them inner classes you are making that more
difficult or even impossible (if you've also made them private).

I'd use inner classes for simple classes which are strictly implementation
(and even then not always). Your classes don't come into that category.

john
 
V

Vinodh Kumar P

John Harrison said:
I don't think so. If you've designed Wheel and FuelTank well its quite
possible that some else will want to use them in the future even if they are
not being used now, by making them inner classes you are making that more
difficult or even impossible (if you've also made them private).

I'd use inner classes for simple classes which are strictly implementation

Can you please give some examples or elaborate on "Strictly Implementation"?
I do not comprehend under what circumstances the inner classes are helpful?
I personally feel nested classes makes the readability of classes
difficult.

And I am at the verge of concluding
"Avoid inner classes.
The runtime layout of any object and the collaboration between objects are
important.
The scoping restriction provided by nested classes are to use in rare
situations."
Am I right?
 
J

John Harrison

Vinodh Kumar P said:
Can you please give some examples or elaborate on "Strictly
Implementation"?

For instance in a singly linked list of integers I might make the node class
an inner class.

class SList
{
struct Node
{
int data;
Node* next;
}
Node* first;
public:
SList();
void insert(int data);
...
};

The user of SList doesn't care about Nodes, nor are Nodes ever likely to be
used in another context. Therefore they are 'strictly implementation'.
I do not comprehend under what circumstances the inner classes are
helpful?
I personally feel nested classes makes the readability of classes
difficult.

That's fair comment.
And I am at the verge of concluding
"Avoid inner classes.
The runtime layout of any object and the collaboration between objects are
important.
The scoping restriction provided by nested classes are to use in rare
situations."
Am I right?

Sounds fair enough to me. In my experience inner classes are used pretty
rarely, other than examples like my list class above.

john
 

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

Staff online

Members online

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top