"Class Name" + ":" Call method, class name call Method

Source: Internet
Author: User

"Class Name" + ":" Call method, class name call Method

Note:

Do not analyze the call methods of static functions or static members;

The test environment mentioned below is vc6.0;

 

The following code is displayed during program debugging:

1 pObj->ClassName::Function();

 

At first, I don't understand why I need to add the class name "ClassName:" after "->". Generally, the class name is added with ":" (ClassName: :) It is used to call static functions or static members. I tried it with questions.

 

Definition Class:

 1 class A 2 { 3   public:  4          void Test() 5          { 6               int nVal= 8; 7               int nVal1 = nVal; 8           } 9 10 };

Call method:

1 A* pA = NULL;2 pA->A::Test();

 

The test is successful. However, the Test () function of Class A found that the this pointer is null during debugging:

 

Modify Class A:

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 the member variable 12 13} 14 15 protected: 16 int m_nVal; // Add the member variable 17 18 };

 

Execute again:

1 A* pA = NULL;2 pA->A::Test();

The program will crash this time. debugging found that it crashed when using member variables, such:

 

 

Through the Test above, it is concluded that when the call is performed using the pA-> A: Test () method, if the Test () function does not use non-static member variables of the class, the call pointer (the specific class Object Pointer this) can be passed without consideration. If the Test () function uses non-static member variables of the class, the pointer to be called must not be null, that is, class objects with allocated memory space must exist, because the space of these non-static member variables is allocated to class objects. Static members of the class are not described here (because the static members of the class belong to the class itself, not the class object ).

 

Now the problem is solved. You just need to write pA-> Test () in the call. Why is it so awkward, it is very esoteric (the fact is a bit esoteric). I guess it should be related to inheritance through experience, so I tried the following:

 

Modify Class:

1 class A 2 {3 public: 4 A () {m_nVal = 5 ;}5 ~ A () {} 6 7 virtual void Test () // convert to virtual function 8 {9 int nVal = 8; 10 int nVal1 = nVal; 11 int nVal2 = m_nVal; 12} 13 14 protected: 15 int m_nVal; 16 };

Adding Class B inherits from Class:

1 class A 2 {3 public: 4 A () {m_nVal = 5 ;}5 ~ A () {} 6 7 virtual void Test () // convert to virtual function 8 {9 int nVal = 8; 10 int nVal1 = nVal; 11 int nVal2 = m_nVal; 12} 13 14 protected: 15 int m_nVal; 16 };

Call method:

A * pA = new B;

PA-> Test (); // Step 1

PA-> A: Test (); // Step 2

 

During debugging, it is found that when the first step is executed, the Test () function defined in Class B (subclass) is entered. When the second step is executed, the Test () function in Class A (parent class) is entered () function, the first step is to implement a common method of polymorphism, while the second method is to deliberately destroy the structure of polymorphism, has reached the designated to execute the parent class function, I personally disagree with this writing method. First, it breaks through the regular Writing Method and seems hard to understand, and is not conducive to maintenance and expansion. It can be implemented in other common ways.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.