this指针的隐式动态绑定

 

遇到了这一段代码:

class CBase
{
public:
    virtual void virfun() //虚函数
    {
        cout<<"base virfun\n";
    }
    void memfun() //非虚
    {
        this->virfun();//this指针永远指向基类,无论其是否有派生类
    }
};
class CDerive:public CBase
{
    virtual void virfun()
    {
        cout<<"derive virfun \n";
    }
};
int main()
{
    CDerive obj;
    obj.memfun();  //通过对象来调用非虚函数memfun,而不是基类指针或引用
    
    cin.get();
}

输出:

image

main函数中,obj对象在memfun中调用的是派生类虚函数。

疑问:

我并没有通过指针或者引用来调用派生类对象,为什么还发生了多态呢?

很多人在调用obj.memfun()的时候发生了切割(派生类对象被切割成基类对象),而非动态绑定。

起始不然,这是因为在调用memfun函数的时候发生了this指针隐式的动态绑定

obj.memfun();                   //注意memfun函数原型为:memfun(CBase *this); (而不是memfun(CBase this)) ,memfun()中的参数this指针永远是指向CBase

obj.memfun()函数在编译器中展开伪码为:

CBase *this =static_cast<CBase *>&obj;       //注意这时候发生了动态绑定,基类this指针指向了派生类对象

memfun(this);                    //注意这时候this指针指向了派生类对象obj,发生了动态绑定,所以在memfun函数体中调用虚函数virfun()的时候,调用的

自然就是派生类对象的virfun()函数了。

posted @ 2014-08-21 10:21  忆寒  Views(376)  Comments(0Edit  收藏  举报