I don't use these very often so let me see if I get this right:
Code:
class Surround
{
int value;
public:
class Within
{
public:
int x;
Within();
};
Surround();
};
- Surround::Within is public
- Within cannot access Surround::value since value is private to Surround and Within is not a friend of Surround
- Surround can access Within::x since Within::x is public
- Within can be accessed from outside of Surround since Within is public and Within's constructor is public, therefore Surround::Within can be instantiated by anyone
- Surround can be instantiated by anyone
The public, protected, and private keywords within Surround refer to how the nested classes will be inherited by derived classes and is not related to nested class access rights in relation to the surrounding class.
So in this derived class:
class MyClass:public Surround { };
MyClass cannot access Surround::value since it is private to Surround.
MyClass inherits Surround::Within as a public class.
MyClass can access Surround::Within::x since x is public in Surround::Within.
Nested classes are quite confusing.