C++ classes: Can i declare a class inside a class?

Started by
13 comments, last by White Rabbit 19 years, 6 months ago
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
"Follow the white rabbit."
Advertisement
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.
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Got it.
Thanks both.
"Follow the white rabbit."
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.
Ah ha, misinterpreted that last question.
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Polymorphic OOP
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.


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

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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++.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
Yes, if you wish to see the exact defect: "C++ Standard Core Language Issue Defect Report #45"

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

This topic is closed to new replies.

Advertisement