Each name in a C + + program must point to a unique entity (such as an object, function, type, or template). But we all know that in a program, the same name often appears pointing to different entities. The mechanism for this implementation is that the compiler has different meanings for names based on the context area. The general context used to distinguish the meaning of a name is the domain (scope). C + + supports three forms of domain: Local domain, namespace domain (namespace scope), and class domain.
For example, in a CPP file, there is a global variable, the relative function is the global domain nested domain, this global variable is visible in all nested fields.
Also like class, there is another class, then the outer layer of the variable in the class is also visible:
#include <iostream>using namespace std; class c1{public: int a; void foo(); class c2 { public: int a; void foo(); } b;};void c1::foo(){ a = 1;}void c1::c2::foo(){ a = 2;} int main(){ class c1 f; f.foo(); f.b.foo(); cout << f.a << endl; cout << f.b.a << endl; return 0;}
C + + variables are visible within nested fields