Call of member functions such as C ++ vs call of common functions
First, see the following statement:
Point3d obj;Point3d *ptr = &obj;
When the above pointer or object is used to call the member function Func (), there will be:
obj.Func();ptr->Func();
What has been done behind the above call?
Assume that the Func function is defined as follows:
Point3d Point3d::Func() const{Float a = getA(); Point3d ret; ret._x = _x/a; ret._y = _y/a; ret._z = _z/a; return ret;}
GetA is defined:
float Point3d::getA(){return sqrt(_x*_x+_y*_y+_z*_z);}
After reading the definitions of these functions, can we know the execution process of the above Code? The answer is no! The above code can tell us that the Func function must not be static. (Because it operates non-static member variables and is a const function ).
So how did the above member function call process be completed?
One of the design principles of C ++ is that the call efficiency of non-static member functions should be consistent with that of non-member functions. Should I declare a function as a member function?
There should be any extra burden. Therefore, the compiler calls a member function as a non-member function.
For example, a non-member function of getA is defined:
float getA(const Point3d *this){ return sqrt(this->_x* this->_x + this->_y * this->_y + this->_z * this->_z);}
Such functions give us the illusion that non-member functions are less efficient to call, because they indirectly obtain the member variables of objects. In the member functions, we use them directly. In fact, this is a hugeMisunderstanding.
Here, we can easily understand that the class member function actually has an implicit form parameter, that is, the this pointer. However, the compiler does this. The Compiler handles the call of member functions following the steps below:
1. Rewrite the function prototype of the member function and express the implicit this pointer. Provide an access pipeline, that is, the member variable of this parameter called in our function. As follows:
Point3d Point3d: Func (Point3d * const this)
Note the position of the const here. The this pointer itself is const. If the member function itself is also a const member function, the function prototype should be
Point3d Point3d: Func (const Point3d * const this)
2. Use the this pointer to indirectly access the member variables in the function. For example:
{
Return sqrt (this-> _ x * this-> _ x + this-> _ y * this-> _ y + this-> _ z * this-> _ z );
}
3. rewrite a member function as an external function. However, mangling is a technology used in C ++ to process overloaded functions of the same name. This makes the function name unique in the program.
The above Func function may be processed as the following name: (different compilers have different processing methods)
Extern Func_Point3dFv (Point3d * const this );
OK! Now, the above obj. Func () call is changed to Func_Point3dFv (& obj ).
The entire process here is a story from the compiler."The call efficiency of member functions must be consistent with that of non-member functions".
[Updated] using the class scope symbol to call a member function (whether a virtual function or a non-virtual function), the compiler's resolution method is consistent with that of the non-static member function.
Using an object to call a member function (whether a virtual function or a non-virtual function), the compiler's resolution method is the same as that of a non-static member function.