A nested class is declared within the scope of another class. The name of a nested class is local to its enclosing class. Unless you use explicit pointers, references, or object names, declarations in a nested class can only use visible constructs, including type names, static members, and enumerators from the enclosing class and global variables.
class A {
int x;
class B { };
class C {
// The compiler cannot allow the following
// declaration because A::B is private:
// B b;
int y;
void f(A* p, int i) {
// The compiler cannot allow the following
// statement because A::x is private:
// p->x = i;
}
};
void g(C* p) {
// The compiler cannot allow the following
// statement because C::y is private:
// int z = p->y;
}
};
int main() { }
The compiler would not allow the declaration
of object b because class A::B is
private. The compiler would not allow the statement p->x
= i because A::x is private. The compiler
would not allow the statement int z = p->y because C::y is
private.class outside
{
public:
class nested
{
public:
static int x;
static int y;
int f();
int g();
};
};
int outside::nested::x = 5;
int outside::nested::f() { return 0; };
typedef outside::nested outnest; // define a typedef
int outnest::y = 10; // use typedef with ::
int outnest::g() { return 0; };
However, using a typedef to represent a nested class name
hides information and may make the code harder to understand. class outnest obj;
class A {
private:
class B { };
B *z;
class C : private B {
private:
B y;
// A::B y2;
C *x;
// A::C *x2;
};
};
The nested class A::C inherits from A::B.
The compiler does not allow the declarations A::B y2 and A::C
*x2 because both A::B and A::C are
private.