A class defined in a class is called a nested class, and a class that defines a nested class is called an external class.nesting of classes is often done to help implement another class and avoid name collisions. Nesting of classes is often done to help implement another class and avoid name collisions. As follows:
class queue
{
private:
struct Node{ Item item;struct Node *next };
...
};
Because structs are members of the public by default,so node is actually a nested class.
The declaration position of a nested class determines the scope of the nested class, that is, it determines the part of the program that can create objects of nested classes. If a nested class is declared in the private part of a class, only the outer class of the nested class can know it. This is the case for the class above. In the queue class, a queue member can use a pointer to a node object or a Node object, but other parts of the program do not even know that the node class exists. For classes derived from the queue, there is no known node. If a nested class is declared in the protected section of a class, it is visible to the outside world. The derived class knows the nested class and can create objects of this type directly. If a nested class is declared in the public part of a class, the latter, the derived class of the latter, and the external world are allowed to use it. Then, when used externally, you must add an outer class scope restriction to the outer class.
class team
{
public
struct Coach{...};
...
};
When using coaches, the Team::coach teach1 should be defined as such;
Nested structures and enumerations are scoped to this same. Many programmers use public enumerations to provide class constants that are used by customers.
The following table summarizes the scope characteristics of nested classes, structs, and enumerations.
Declaration Location |
Whether the class containing it can use it |
Whether a class derived from the class that contains it can use it |
Whether it can be used externally |
Public |
Is |
Is |
Is |
Private |
Is |
Whether |
Whether |
Protection |
Is |
Is |
Whether |
Declaring a nested class in an external class does not give the external class any access to the nested class, nor does it give any nested classes access to the external class. Same as general class access control (private, public, protected).
Accessing nested classes in an external class
class test
{
public:
test()
{
i = 10; //不能访问
mytest::i = 10;//不能访问
}
private:
class mytest
{
int i;
int j;
};
};
You cannot directly access the members of MyTest and try to access them through objects.
class test
{
public:
test()
{
cc.i = 10; //通过对象可以访问,如果i为私有则不可访问
}
private:
class mytest
{
public:
int i;
int j;
};
mytest cc;
};
By testing, you declare an object of a nested class in an external class. The object is then used in the outer class to access the nested class, with the same rules as the normal class.
The external class is accessed in a nested class because there is no access to the outer class in the nested class. Therefore, only the modified object is defined in the nested class to be able to access its non-static members, but at this point the outer class is an incomplete type (the class does not have a definition complete). Therefore, it is not possible to access external classes within nested classes.
From for notes (Wiz)
C + + nested classes (inner and outer classes)