On 21 Maj, 13:34, Nike <pra.ra...@gmail.comwrote:
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);
}
--
Erik Wikström