Member function, virtual function, this pointer

Source: Internet
Author: User

1. There is no essential difference between the implementation mechanism of member functions and normal (global) functions. For the compiler, after name processing (add the namespace and class name before the function name ), A member function is a common function and has a fixed function body and entry address in the compiled code area. The biggest difference is that when a call occurs, for a member function, the compiler will implicitly push this statement, where this pointer points to the object address, which is the data required for the operation. The compiled member function call may be:
...
Push this
Call F
...
Therefore, when sizeof is used for a class, the space occupied by member functions cannot be seen, because the entry address is only determined by the compiler during function calling, the class bucket does not need to record any information about member functions.

2. static member functions are actually common functions, but the compiler limits the visible range of their names (because the class name is added before the function name during compilation ). Therefore, when calling a static member function, you do not need to push this pointer. You only need to add the class name qualifier before the function name.

3. Based on the above content, you can better understand the dynamic binding of virtual functions. When a virtual function is defined in a class, the compiler will define a virtual table for the class, which records the entry addresses of all virtual functions. At the same time, a virtual pointer will be inserted into the data member of the class to point to this virtual table (the V-PTR must be set after the base class constructor is called. Because if the inherited class has its own virtual function table, the V-PTR will be rewritten to point to the table, even if the previous V-PTR has been set by the base class ). This is why defining a member function does not change the size of sizeof (class), and defining one or more virtual functions will make sizeof (class) increase by 4 (the storage space of the virtual pointer ). When calling a virtual function, use the same policy as calling a member function.
This means that the entry address of the corresponding virtual function in the virtual table is obtained based on this pointing to the object, so that dynamic binding is implemented without determining the entry address during compilation like a member function.

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.