Analysis of class member function calls in c ++

Source: Internet
Author: User

Here is a small example to illustrate how member functions are called in c ++.


# Include
 
  
Using namespace std; class Test {public: void funcMember () {cout <"I'm member function" <endl; int * ptr = reinterpret_cast
  
   
(Const_cast
   
    
(This); // int * ptr = (int *) (const_cast
    
     
(This); cout <* ptr <endl; // Why is it int x; int y; there are eight bytes of cc in the middle of the stack, which should be x, and y is next to ptr + = 3; // Therefore, three cout <* ptr <endl; ptr + = 3; cout <* ptr <endl; ptr + = 3; cout <* ptr <endl; ptr + = 3; cout <* ptr <endl;} Test (): x (3) {} int x ;}; int main () {Test * t = NULL, * ptr; int x = 10; int y = 20; int z = 30; int zz = 40; Test tt; // t-> funcMember (); ptr = & tt; ptr-> funcMember (); cout <"------------------------" <endl; tt. funcMember (); return 0 ;}
    
   
  
 



First, let's explain why t is a null pointer to the commented out part. This call is still successful.

Please refer to the results after Disassembly


Therefore, the first part of the red line is omitted.


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + xMfU2b + 0sbvXosrNtfS687PM0PK1xNTL0NC94bn7yOe6zsTYo788L3A + CjxwPjxpbWcgc3JjPQ = "http://www.2cto.com/uploadfile/Collfiles/20140509/20140509091953256.jpg" alt = "">

It can be seen from the results that the instance of the object is passed into the function as an address, so as long as this address is not used, no matter whether it is empty or not, it will not be wrong, if you use what you have done in this example,

You can also flexibly change the cause.

Related Article

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.