Nested class with public access????

N

Nike

I have a small question w.r.t nested class..
I see in many codes nested classes are declared as public..
First, of all my understanding is that you go in for a nested class if
you are sure that this class cannot exist on it's own..
So , generally this should be declared as private so that none can
instantiate this openly..
I need to know the following..
a)Is there any design advantage of having nested class as Public???
class A{
public:
class B{
};
B b;
}
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

I have a small question w.r.t nested class..
I see in many codes nested classes are declared as public..
First, of all my understanding is that you go in for a nested class if
you are sure that this class cannot exist on it's own..
So , generally this should be declared as private so that none can
instantiate this openly..
I need to know the following..
a)Is there any design advantage of having nested class as Public???

I'd say it's useful to indicate that a class is (as you said) strongly
connected with another. Consider for example an implementation of a
list:

class List {
public:
struct Node
{
Node* prev;
Node* next;
};

private:
Node* first;
};

However in many cases you might not want to make the implementation
public so you would perhaps make it private. Another use (though not
for classes) is for enums:

class Foo {
public:
enum Type{ One, Two };
void setType(Type);
};

int main() {
Foo f;
f.setType(Foo::Two);
}
 

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
474,294
Messages
2,571,511
Members
48,206
Latest member
EpifaniaMc

Latest Threads

Top