Nonstatic Member Function 的语意

C++ 的设计准则之一就是: nonstatic member function 至少必须和一般的 nonmember function 有相同的效率。 这就是说, 如果我们在以下两个函数之间做选择:

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

那么选择 member function 不因该有什么额外负担, 这是因为编译器内部已将 member 函数实体转换为对等的 nonmember 函数实体。
举个例子, 下面是 magnitude() 的一个 nonmember 定义:

float magnitude3d(const Point3d *_this)
{
    return sqrt(_this->_x *this->_x +
            _this->_y *this->_y +
            _this->_z *this->_z);
}

乍看之下似乎 nonmember function 比较没有效率, 它间接地经由参数取用坐标成员, 而 member function 却是直接取用坐标成员。 但是实际上 member function 被内化为 nonmember 的形式。以下是转换步骤:
1. 改写函数的 signature 以安插一个额外的参数到 member fucntino 中,用以提供一个存取管道, 使 class object 得以调用该函数。该额外函数被称为 this 指针:

//non-const nonstatic member 的增长过程
Point3d
Point3d::magnitude(Point3d *const this)
//如果 memberfunction 是 const, 则变成:
//const nonstatic member 的扩张过程
Point3d
Point3d::magnitude(const Point3d *const this)

2. 将每一个对 nonstatic data member 的存取操作改为经由 this 指针来存取:

{
    return sqrt(_this->_x *this->_x +
            _this->_y *this->_y +
            _this->_z *this->_z);
}

3. 将 member fucntion 重现写成一个外部函数, 对函数名称进行 mangling 处理, 使它成为程序中独一无二的语汇:

extern magnitude__7Point3dFv(
    register Point3d *const this);

转换好了之后其每个调用操作也需要转换, 于是:

//你看到的
obj.magnitude();
//转换为
mgnitude__7Point3dFv( &obj );

//你看到的
ptr->magnitude();
//变成了:
magnitude__7Point3dFv(ptr);

//你看到的
Point3d
Point3d::normalize() const
{
    register float mag = magnitude();
    Point3d normal;

    normal._x = _x / mag;
    normal._y = _y / mag;
    normal._z = _z / mag;
    return normal;
}
//内部转化为
void normalize__7Point3dFv(register const Point3d *const this,
               Point3d &__result)
{
    register float mag = this->magnitude();
    //default constructor
    __result._x = this->_x / mag;
    __result._y = this->_y / mag;
    __result._z = this->_z / mag;
    return;
}
//另一个比较有效率的方法
Point
Point3d::normalize() const
{
    register float mag = magnitude();
    return Point3d( _x/mag, _y / mag, _z / mag);
}
//这个方法会被转化为
void
normalize__7Point3dFv( register const Point *const this,
            Point3d &__result)
{
    register float mag = this->magnitude();
    __result .Point3d::Point3d(
        _x/mag, _y / mag, _z / mag );
    return;
}

这可以节省 default constrctor 初始化所引起的负担。

时间: 2024-08-28 14:42:15

Nonstatic Member Function 的语意的相关文章

invalid use of non-static member function ‘void Date::init(int, int, int)’

#include<iostream> using namespace std; class Date { public: int day,month,year; void init(int,int,int); void print_ymd(); }; void Date::init(int yy, int mm, int dd) { year = yy; month = mm; day = dd; } void Date::print_ymd() { std::cout << ye

类非静态成员的函数指针 的使用 Function pointer of a non-static member function of a class

you can get the pointer of the method, but it has to be called with an object typedef void (T::*MethodPtr) (); MethodPtr method = &T::MethodA; T *obj = new T(); obj->*method(); If you need to have non-object pointer and you want to use object then

Virtual Member Functions &amp; Static Member Function

如果一个 normalize() 是一个 virtual member function, 那么以下的调用: ptr->normalize(); 将会被内部转化为: (*ptr->vptr[1])(ptr); 其中:vptr 表示由编译器生成的指针, 指向 virtual table, 它被安插在每一个声明有(或继承自) virtual functinos 的 class object 中. 事实上其名称也会被 mangled, 因为在一个复杂的 class 派生体系中, 可能存在多个 vpt

C++对象模型——指向Member Function的指针 (Pointer-to-Member Functions)(第四章)

4.4 指向Member Function的指针 (Pointer-to-Member Functions) 取一个nonstatic data member的地址,得到的结果是该member在 class 布局中的byte位置(再加1),它是一个不完整的值,须要被绑定于某个 class object的地址上,才可以被存取. 取一个nonstatic member function的地址,假设该函数是nonvirtual,则得到的结果是它在内存中真正的地址.然而这个值也是不全然的,它也须要被绑定

Timer.4 - Using a member function as a handler

In this tutorial we will see how to use a class member function as a callback handler. The program should execute identically to the tutorial program from tutorial Timer.3. #include <iostream> #include <boost/asio.hpp> #include <boost/bind.

Reading Effictive java: static member class (SMC) and nonstatic member class(NSC)

Misunderstanding of static member class : For most programmers, comparing to SMC ,we may be more familiar with static field . so we may analogously guess how SMC works according to static field's feature. That leads to a misunderstanding.--SMC may on

Fatal error: Call to a member function read() on a non

选模版时报错如下:Fatal error: Call to a member function read() on a non-object inC:\wamp\www\DedCms5.7\include\dialog\select_templets.php on line 71或者是/include//templets/default/index.htm Not Found!95%的导致原因是本地测试好了之后上传到服务器的路径问题!解决方法:后台->系统->系统基本设置->核心设置 -

&#39;init&#39;:member function definition looks like a ctor(构造函数的缩写)

具体编译的错误提示如下:warning C4183: 'init': member function definition looks like a ctor, but name does not match enclosing classwarning C4183: 'method': member function definition looks like a ctor, but name does not match enclosing classLinking...main.exe -

ECmall错误:Call to a member function get_users_count() on a non-object

问题描述: 在后台添加了一个app报错:Call to a member function get_users_count()Fatal error: Call to a member function get_users_count() on a non-object in \includes\ecapp.base.php on line 问题原因有一下几点: 1.你的APP文件没有对应的Lang文件: 2.你的APP文件和Lang文件如果是UTF编码的,可能是有BOM: 3.你的APP文件和