Jump to content
  • Advertisement
Sign in to follow this  
White Rabbit

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

This topic is 5469 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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++.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!