Note:
The method of calling static or static members is not analyzed;
The test environment mentioned below is vc6.0;
You see this code when you debug a program:
1 pobj->classname::function ();
Beginning to understand why to add the class name "ClassName::", generally use the class name plus "::" (ClassName::) is used to call static functions or static members, with a question to do the next attempt.
Define Class A:
1 class A 2 {3 public : 4 void Test () 5 {6 int nval= 8; 7 int nVal1 = nval; 8 } 9 10};
Call Mode:
1 * PA = null;2 pa->a::test ();
The test can pass smoothly. However, the test () function in Class A when debugging is found to be null for this pointer:
Modify Class A To:
1 Class A 2 {3 public:4 a () {m_nval = 5;} 5 ~a () {} 6 7 void Test () 8 {9 int nval= 8;10 int NVal1 = nval;11 int nVal2 = m_nval; Call member variable }14 protected:16 int m_nval; Increase member variable 17 18};
Execute again:
1 * PA = null;2 pa->a::test ();
This time the program crashes and the debug discovery crashes when using member variables, such as:
The above test preliminarily concludes that when a call is made using Pa->a::test (), if the test () function does not use a non-static member variable of the class, the calling pointer (the specific class object pointer this) is empty can be passed without consideration if the test A non-static member variable for a class is used in the function, you must require that the calling pointer is not NULL, that is, there must be a class object that has allocated memory space, because the space for these non-static member variables is allocated on the class object. The static members of a class are not described here (because the static members of the class belong to the class itself and do not belong to the class object).
Here comes the question, the call is directly written pa->test () can not, why write so awkward, give a person feel very abstruse appearance (the fact is a bit abstruse), through experience guess should and inheritance has relations, so did the following attempt:
Modify Class A:
1 Class A 2 {3 public:4 a () {m_nval = 5;} 5 ~a () {} 6 7 virtual void Test () //Become virtual function 8 { 9 int nval= 8;10 int nVal1 = nval;11 Int. NVal2 = m_nval;12 }13 protected:15 int m_nval;16};
Increase class B to inherit from Class A:
1 Class A 2 {3 public:4 a () {m_nval = 5;} 5 ~a () {} 6 7 virtual void Test () //Become virtual function 8 {9 in T nval= 8;10 int nVal1 = nval;11 Int. NVal2 = m_nval;12 }13 protected:15 int m_nval;16};
Call Mode:
A * PA = new B;
Pa->test (); The first step
Pa->a::test (); Step Two
Debugging found that when the first step into the Class B (subclass) defined in the test () function, the second step into the Class A (parent Class) in the test () function, the first call is a way to implement polymorphism is very common, and the second method of invocation is deliberately destroy polymorphic structure, has reached the specified function to execute the parent class, I personally do not approve of this type of writing, first it breaks the general wording, and it seems difficult to understand, it is not conducive to maintaining the extension, can take other common ways to achieve the corresponding function.
"Class name" + "::" Call method