C++ classes: Can i declare a class inside a class?
Hi.
I'de like to know if i can declare a class inside another class, and what will result from me doing so instead of declaring it outside.
Will i only be able to create variables of that class type inside the class it was declared in?
Will the information of the inner class be accessible by the outter class methods?
Thanks
Yes. The only difference is that other classes will need to use extra notation, they'll have to say OuterClass::InnerClass in order to reference the inside class.
Any outside class can create instances of the inner class, unless you make the inner class 'private' or 'protected'
As far as information being accessible- it works just like a normal class, so the usual rules apply. You need to have an actual instance of the inner class in order to access data, or you can make the inner class's data static.
It doesn't work like Java, where inner classes have an implied link to the outer class that created them.
Any outside class can create instances of the inner class, unless you make the inner class 'private' or 'protected'
As far as information being accessible- it works just like a normal class, so the usual rules apply. You need to have an actual instance of the inner class in order to access data, or you can make the inner class's data static.
It doesn't work like Java, where inner classes have an implied link to the outer class that created them.
Quote:Original post by White Rabbit
I'de like to know if i can declare a class inside another class
Yes.
Quote:and what will result from me doing so instead of declaring it outside.
If the outer class isn't a class template, about the only difference is in access control. If the outer class is a class template... things get a bit more complex.
Quote:Will i only be able to create variables of that class type inside the class it was declared in?
Depends on what access qualifier (public, protected, private) you used.
Quote:Will the information of the inner class be accessible by the outter class methods?
Not unless you make the outer class a friend of the inner class.
Quote:Original post by White Rabbit
Will the information of the inner class be accessible by the outter class methods?
As was said, no, however, the opposite is true -- the inner class does have access to the outer class's private and protected data as though it were implicitly a friend.
Quote:Original post by Polymorphic OOP
As was said, no, however, the opposite is true -- the inner class does have access to the outer class's private and protected data as though it were implicitly a friend.
Incorrect.
Quote:Original post by Polymorphic OOPQuote:Original post by White Rabbit
Will the information of the inner class be accessible by the outter class methods?
As was said, no, however, the opposite is true -- the inner class does have access to the outer class's private and protected data as though it were implicitly a friend.
Not true. In the current revision of the standard, nested classes cannot access private members of the outer class (11.8.6-1). Nor can they be declared friend classes, the exact quote being: "A friend of a class is a function or class that is not a member of the class..." 11.4.6-1
Quote:Original post by Polymorphic OOP
As was said, no, however, the opposite is true -- the inner class does have access to the outer class's private and protected data as though it were implicitly a friend.
As I recently was informed, not according to the standard (although it's apparently been proposed for the next revision). However, I've had it work just fine in both gcc and vc++.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement