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.