"Deep Exploration C + + object Model" Reading notes (4)

Source: Internet
Author: User

Non-static member functions (nonstatic-FUNCTIONS) * * *

One of the design guidelines for C + + is that the nonstatic member function must be at least as efficient as the normal nonmember function. In other words, if we were to choose between the following two functions:

float magnitude3d(const Point3d *this) { ... }
float Point3d::magnitude3d() const { ... }

Then choosing the member function should not impose any additional burdens. Because the "member function entity" has been converted into a peer "nonmember function entity" internally by the compiler. The following is a nonmember definition for magnitude ():

float Pointer3d::magnitude() const
{
return sqrt(_x*_x + _y*_y + _z*_z);
}
// 内部转化为
float magnitude_7Point3dFv(const Point3d *this)  //已对函数名称进行 “mangling”处理
{
return sqrt(this->_x*this->_x + this- >_y*this->_y + this->_z*this->_z);
}

Now, every invocation of the function must also be converted:

obj.magnitude();
// 转换为
magnitude_7Point3dFv (&obj);

For memeber in class, simply add a class name to the name of the member to form a unique name. However, since the member function can be overloaded, a broader mangling approach is needed to provide an absolutely unique name. One approach is to encode the types of the parameters in their parameter lists.

class Point {
public:
void x(float newX);
float x();
...
};
// 内部转化为
class Point {
void x_5PointFf(float newX);  // F表示function,f表示其第一个参数类型是float
float x_5PointFv();  // v表示其没有参数
};

The above mangling method can check out any improper invocation operations during the link period, but because the return type is not considered in the encoding, it cannot be checked if the return type declares an error.

Virtual member function (Avatar functions) * * *

For objects that do not support polymorphism, a virtual function is invoked via a class object, which should always be resolved by the compiler as it treats the general nonstatic member function:

// Point3d obj
obj.normalize();
// 不会转化为
(*obj.vptr[1]) (&obj);
// 而会被转化未
normalize_7Point3dFv(&obj);

static member function (functions) * *

Before introducing the static member functions, C + + requires that all member functions must be invoked through the object of that class. In fact, without any nonstatic data members being accessed directly, there is no need to invoke a member function through a class object. This creates a paradox: on the one hand, the static data Member declaration of nonpublic is a good habit, but it also requires that it must provide one or more member functions to access the member, while you can access a static member without class object. But its access function is bound to the class object.

The static member functions came into being in this situation.

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.