1, the introduction of the problem
1). Observe the following first block of code:
#include <iostream>using namespace Std;class screen{public: void Test () { DUMMY_FCN (); } void Dummy_fcn () { }};
This code compiles with no errors ..... .....
2). Look at the following second block of code:
#include <iostream>using namespace Std;void Test () { DUMMY_FCN ();} void Dummy_fcn () {}
There was an error compiling this code ....
3). Then look at the third block of code:
#include <iostream>using namespace Std;void dummy_fcn () {}void test () { DUMMY_FCN ();}
This code compiles without errors .....
4). Fourth Block code:
#include <iostream>using namespace Std;class screen{public: typedef std::string::size_type Index; void Test () { DUMMY_FCN (); } void Dummy_fcn () { cursor = width * height; } Private: index cursor; index height, width;};
there is no error in compiling this code ......... .....
5). Fifth Block Code:
#include <iostream>using namespace Std;class screen{public: void Test () { DUMMY_FCN (); } void Dummy_fcn () { cursor = width * height; } Private: index cursor; index height, width; typedef std::string::size_type Index;};
error compiling this code .....
2. DiscoveryDiscovery 1: Comparing the 1th, 2, and 3 blocks of code, we found that in class, the name lookup is more special. For normal function names, the compiler will only look for declarations from where they were before, and if there is no declaration, an error (such as code 2). For names in a class, the compiler will look for the entire class scope (code 1, for example), and if it is not found, it will be further searched before the definition of the entire class.
Discovery 2: Comparing the 4th and 5 blocks of code, the difference is only in the position of the index type variable. It is known that when the compiler compiles the definition of a class, it does not look for the definition of the type (in this case, the definition of index) within the scope of the class.
3. ThinkingThe above two-point discovery is actually related to the "two-step compilation" Of the class definition: See P382 in version 4th of the C + + primer, "class definitions are actually handled in two phases: (1) First, compile member declarations, (2) compile their definitions themselves only after all members have occurred. "Where Discovery 2 corresponds to the first step in" two-step compilation "(called the name lookup for class member declarations in C + + primer), and Discovery 1 corresponds to the second step (the name lookup in the C + + primer called the class member definition).
3.1 Thinking of the first step of compilingIn the first step of compiling all member declarations, the function calls in the member function definition are not checked (as in the example found in 2). A check is made only on the definition of the class name in the member declaration (such as index in the previous example), which is divided into two steps: 1) find before the class name is used in a Class 2) before the entire class definition
3.2 Second-step compilation thinkingWhen you compile their definitions in the second step, the function calls in the member functions are examined further (such as the example found in 1). At this point the check is divided into three steps: 1) in the member function local scope to find 2) in the entire class definition field to find. The ability to find in the entire class definition domain is due to the first step of defining all class member declarations. This is also different from the normal name lookup (as found in 1 example) 3) before this class member function definition (this is not just before the class definition, because the class member function may be outside the class) to find
Thinking of name Lookup in class scope ("C + + Primer", fourth edition P382)