(1) Introduction:
An inner class is actually a kind of local data type defined within a class declaration. (very similar to the struct node declaration), which differs from Java's.
Declarations of----inner classes are of public and private
If declared as public, it can also be used outside to define variables, such as Outer::inner var
If the declaration is private, then the outside can not be used to define the variable, then outer::inner var will cause a compilation error.
(2)----The internal class declaration can be used to define the variable
This is the same as other data type definition variables, and access rules are the same. Without him
----Mutual access of internal classes and external classes
cannot be accessed and is completely dependent on the definition property of the member variable.
(3) code implementation (see note below, there is not much to explain here)
#include <iostream>using namespace Std;class a{public:class b1{public:int A; Private:int b; public:void foo (A &p) {cout << p.i1 << Endl; OK, because I1 is public in class A cout << p.i2 << Endl; Fail, because I2 is private in class A}}; Private:class B2 {Public:int A; Private:int b; public:void foo (A &p) {cout << p.i1 << Endl; OK, because I1 is public in class Acout << p.i2 << Endl; Fail, because I2 is private in class A}}; PUBLIC:B1 B11; B2 b12;int i1;private:b1 B21; B2 b22;int i2;public:void F (b1& p) {cout << p.a << Endl; OK, because a is public in class B1 cout << p.b << Endl; Fail, because B is private in class B1} void F (b2& p) {cout << p.a << Endl OK, because a is public in class B2cout << p.b << Endl; Fail, because B is private in class B2}};int main (int argc, char *argv[]) {a A; A::B1 AB1; OK, because B1 is declared as public inner class. A::B2 ab2; Fail, because B2 is declared as private inner class return 0;}
C + + Internal classes and external classes (Java)