What Is a Nested Class?
A class defined in a class body is called a nested class. A class that has nested classes is called a peripheral class.
Nested class
Right!
- Chinese name
- Nested class
- Foreign name
- Nested Class
- Subject
- computer science and Technology
- category
- C ++ programming
- Nested classes are only types declared inside the outer class, not members of the outer class
- Outer class does not have members defined by nested classes
- For the outer class, the nested class is no different from other classes; the outer class can access the public members of the nested class object, and cannot access the protected and private members of the nested class object.
- Nested class does not have members defined by surrounding classes
- The nested class can access the public members of the outer class, and cannot access the protected and private members of the outer class.
- Whether the nested class is declared as public, protected, or private, the nested class is visible to any member of the surrounding class
- When a nested class is declared public in a peripheral class, the scope outside the peripheral class is visible, and the scope outside the peripheral class can access the nested class through "::";
- A class defined in a class body is called a nested class. A class that has nested classes is called a peripheral class.
- A class defined in a class body is called a nested class. A class that has nested classes is called a peripheral class. [1]
- The original purpose of defining a nested class is to create a class that can only be used by a certain type. The purpose is to hide the class name and reduce the global identifier, thereby restricting whether the user can use the class to create objects. This can improve the abstract ability of the class and emphasize the master-slave relationship between the two classes (peripheral and nested classes). [1]
#include <iostream> using namespace std; class A { public: class B { public: B (char * name) { cout << "constructing B:" << name << endl; } void printB (); }; B b; A (): b ("In class A") { cout << "constructing A" << endl; } }; void A :: B :: printB () { cout << "B's member function" << endl; } int main (int argc, char * argv []) { A a; A :: B b ("outside of A"); b.printB (); }
- Program output results:
- constructing B: In class A
- constructing A
- constructing B: outside of A
- B's member function
- 2.1 Nested classes For surrounding classes:
- 2.2 Peripheral classes For nested classes:
- 2.3 Scope of a nested class outside the surrounding class:
- (1) Only private members of a class can access private members of a class, so peripheral classes cannot access private members of a nested class. Nested classes can access members of the surrounding class (through objects, pointers, or references).
- (2) Nested classes can be either private or public. In the above example, the access permission of the nested class B is public, and the nested class can be used outside the member functions of the outer class, and the name is qualified when used. If the access permission of nested class B is set to private, it can only be used within the peripheral class. [2]
- (3) A member function in a nested class can be defined outside its class.
- (4) The nested class can directly access the static members, type names (typedef), and enumeration values of the surrounding classes.
- Taking the access of a nested class to a static member variable of an outer class as an example, the access method is directly accessed through "ClassName :: staticVarName".