C + + Primer Learning notes _17_ classes and Data Abstraction (3) _ Class scope
Introduction:
Each class defines its own new scope and unique type. Even though two classes have exactly the same list of members, they are different types. the members of each class differ from the members of any other class (or any other scope) .
First, the name lookup in the scope of the class
1) First, look for the name declaration in the block that uses the name. Consider only the name that was declared before the item was used.
2) If it is not found in 1, it is found in the bounding scope.
If no claims are found, an error occurs.
The definition of a class is actually handled in two phases:
1) First, the compiler declares;
2) The definitions themselves are compiled only after all members have appeared .
The identifiers described in the class scope are only visible in the class. In addition to the class scope, there are block scopes, file scopes, function prototype scopes, function scopes, for example:
#include <iostream>using namespacestd; Class test{public: int num;};//num = ; The scope of the error,num is within the class int num =20; The scope of NUM is the file scope, which is different from the scope of NUM in the class int add (int a,int b); A, the scope of the B two identifier is the function prototype scope int main (void) { int num = five; Num is the block as field { int num = +;//num is block as domain } cout << num << endl; cout <<::num << Endl; return 0;} int add (int a,int b) //parameter A and B are also considered block scope { return a + b;}
int Test () {LABEL1://function scope cout << "Label1" <<endl; Goto LABEL3; LABEL2: cout << "Label2" <<endl; Goto LABEL1; LABEL3: cout << "Label3" <<endl; Goto LABEL2;}
1. Name lookup for class member declarations
type names must be defined in a class before they can be used as the type of a data member, or as a return type or parameter type of a member function. Once a name is used as the type name, the name cannot be defined repeatedly:
typedef double MONEY;CLASS Account{public: Money balance () { return bal; } private: typedef Long double money; Error money Bal;};
2. Name lookup in class member set
1) First check the declaration in the member function local scope .
2) If a declaration of that name is not found in the member function, the declaration of all class members is checked.
3) If a declaration of that name is not found in the class, the declaration that appears in the scope before this member function definition is checked.
3, class members follow the regular block action domain name Word lookup
try not to use formal parameters and members of the same name in your program:
int Height;class screen{public: typedef int index; Because height is defined in the function parameter, //So the height here is masked by a member named height of void DUMMY_FCN (index height) { cursor = width * Height; } Private: index cursor; index height,width;};
Although members of a class are masked, you can still use the class name to qualify the member name or use the this pointer explicitly:
void DUMMY_FCN (index height) { cursor = width * this--height; These two statements function the same cursor = width * screen::height; }
4. After the function scope, look in the scope of the class
If you want to use a height member, the best way to do this is to define a different name for the parameter:
void DUMMY_FCN (Index ht) { cursor = width * height; }
Although the height is used first in DUMMY_FCN and then declared, the compiler is still determined to use a data member named height.
5. After the class scope, look in the perimeter scope
Although the global object height is masked by the data member height in the class, you can still use it by qualifying the name with the global scope operator :
void DUMMY_FCN (index height) { cursor = width *:: Height }
6, nested class, Local class
(1), nested class
The perimeter class needs to use a nested class object as the underlying implementation, and the nested class is used only for the implementation of the perimeter class, and the underlying implementation can be hidden from the user at the same time.
From a scope perspective, nested classes are hidden within the perimeter class, and the class name can only be used in a perimeter class. If you use the class name in a scope other than the perimeter class, you need to add a name qualifier.
A member function in a nested class can be defined outside its class body.
The member functions of a nested class do not have access to the private members of the perimeter class, and vice versa.
Nested classes are just syntax embedding
(2), partial class
A class can also be defined within a function body, such that a class is called a local class (Localclass). A local class is visible only within the local domain in which it is defined.
The member functions of a local class must be defined in the class body.
A local class cannot have static members, about static members and static member functions in a class.
#include <iostream>using namespace std; Class Outer{public: class Inner {public : void Fun (); { // cout<< "Inner::fun ..." <<endl; } };p ublic: Inner obj; void Fun () { cout << outer::fun ... "<< Endl; Obj. Fun (); }}; void Outer::inner::fun () { cout << "Inner::fun ..." << Endl;} void Fun () { class Localclass {public : int num_; void Init (int num) { num_ = num; } void Display () { cout << "num=" << num_ << Endl; } static int num2_; Static members cannot be defined inside a local class }; Localclass LC; Lc. Init (ten); Lc. Display ();} int main (void) { Outer o; O.fun (); Outer::inner i; I.fun (); Fun (); Localclass LC; Error, the local class can only use return 0 in the body of the function in which it is defined ;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + Primer Learning notes _17_ classes and Data Abstraction (3) _ Class scope