虚函数相关问题探索

虚函数相关问题探索

本篇文章中对虚函数做五个方面的探索。
1) 虚函数单一继承对象模型。
2) 虚表指针与虚表的创建释放时机。
3) 析构函数设置为虚函数。
4) 构造函数调用虚函数。
5) 析构函数调用虚函数。

1. 虚函数单一继承对象模型
参见网址:http://www.cnblogs.com/taoxu0903/archive/2008/02/04/1064234.html
参考书:《C++对象模型》。
虚函数实现机制:采用晚绑定机制。类中包含虚表指针成员变量,虚表指针执行虚函数地址表。
检测代码:

class Base
{
public:
    virtual void x();
    virtual void y();

private:
    int ival;
};

class Derived: public Base{
public:
    void x();
    virtual void z();

private:
    long lval;
};

对应内存对象模型如下。
这里写图片描述

2. 虚表指针与虚表的创建释放时机
结论:虚表和虚表指针在构造函数时创建,在析构函数中释放。
我们追踪一下创建与释放时机。
代码:

#include "stdafx.h"
#include <iostream>
using namespace std;

class Base
{
public:
    Base()
    {
        this;
    }

    virtual void Print()
    {

    }

    virtual ~Base()
    {
        this;
    }
};

class Derived : public Base
{
public:
    Derived()
    {
        this;
    }

    virtual void Print()
    {

    }

    ~Derived()
    {
        this;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Base* pB = new Derived();
    delete pB;
    pB = NULL;
    return 0;
}

断点跟踪虚表指针流程如下。
1) Base::Base()
初始化Base类虚表,虚表指针指向Base类虚表0x00de7840。
这里写图片描述
2) Derived::Derived()
初始化Derived类虚表,虚表指针指向Derived类虚表0x00de7834。
这里写图片描述
3) Derived::~Derived()
释放Derived类虚表,虚表指针指向Base类虚表0x00de7840。
这里写图片描述
4) Base::~Base()
释放Base类虚表,释放虚表指针。
3. 析构函数设置为虚函数
先说结论:析构函数如果不设置为虚函数,会有继承类的虚函数未被调用的危险。
建议:析构函数都设置为虚函数。
检测代码:

class Base
{
public:
    Base()
    {
        cout<<"Base"<<endl;
    }

    ~Base()
    {
        cout<<"~Base"<<endl;
    }
};

class Derived : public Base
{
public:
    Derived()
    {
        cout<<"Derived"<<endl;
    }

    ~Derived()
    {
        cout<<"~Derived"<<endl;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Base* pB = new Derived();
    delete pB;
    return 0;
}

输出结果:
Base
Derived
~Base
可以看到继承类的析构函数没有被调用。
将Base的析构函数设置为虚函数

virtual ~Base()
{
    cout<<"~Base"<<endl;
}

输出结果正常:
Base
Derived
~Derived
~Base
4. 构造函数调用虚函数
问题:构造函数中是否可以调用虚函数?
答案:可以调用,但不是我们所期待的行为,调用虚函数只是当前类的函数。
建议:构造函数中尽量不要调用虚函数。
参见C++箴言:避免构造或析构函数中调用虚函数。
代码:

#include "stdafx.h"
#include <iostream>
using namespace std;

class Base
{
public:
    Base()
    {
        cout<<"Base"<<endl;
        Print();
    }

    virtual void Print()
    {
        cout<<"Base::Print()"<<endl;
    }

    virtual ~Base()
    {
        cout<<"~Base"<<endl;
    }
};

class Derived : public Base
{
public:
    Derived()
    {
        cout<<"Derived"<<endl;
        Print();
    }

    virtual void Print()
    {
        cout<<"Derived::Print()"<<endl;
    }

    ~Derived()
    {
        cout<<"~Derived"<<endl;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Base* pB = new Derived();
    delete pB;
    return 0;
}

输出结果:
Base
Base::Print()
Derived
Derived::Print()
~Derived
~Base
分析:构造函数调用虚函数时,此时虚表指针指向基类虚表,故构造函数调用虚函数是调用基类函数。
5. 析构函数调用虚函数
问题:析构函数中是否可以调用虚函数?
答案:可以调用,但不是我们所期待的行为,调用虚函数只是当前类的函数。
建议:析构函数中尽量不要调用虚函数。
代码:

#include "stdafx.h"
#include <iostream>
using namespace std;

class Base
{
public:
    Base()
    {
        cout<<"Base"<<endl;
    }

    virtual void Print()
    {
        cout<<"Base::Print()"<<endl;
    }

    virtual ~Base()
    {
        Print();
        cout<<"~Base"<<endl;
    }
};

class Derived : public Base
{
public:
    Derived()
    {
        cout<<"Derived"<<endl;
    }

    virtual void Print()
    {
        cout<<"Derived::Print()"<<endl;
    }

    ~Derived()
    {
        Print();
        cout<<"~Derived"<<endl;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Base* pB = new Derived();
    delete pB;
    return 0;
}

输出结果:
Base
Derived
Derived::Print()
~Derived
Base::Print()
~Base

分析:派生类析构执行后,虚表指针指向了基类虚表,故基类中再调用虚函数将会调用到基类的函数而非派生类的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值