大多数派生类的析构函数中的纯虚拟调用 [英] Pure virtual call in destructor of most derived class

查看:124
本文介绍了大多数派生类的析构函数中的纯虚拟调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你不应该在基类的ctor或dtor中调用任何虚拟函数,但是从最派生类的角度来看呢?应该罚款吧?例如

I know you shouldn't call any virtual function in the ctor or dtor of the base class, but what about from that of the most derived class? Should be fine right? E.g.

class base {
    ...
    virtual void free() = 0;
};
class child : public base {
    ...
    free() {/* free memory */}
    ~child() {free();}
};


推荐答案

* c>,而不是任何更多派生。所以当你有另一个导出类类foo:child 覆盖 free() em 。

Well, you can do it, but the dynamic type of *this inside child::~child() is child, and not anything more derived. So when you have a further derived class class foo : child which overrides free(), then the overridden function will not be called.

这篇关于大多数派生类的析构函数中的纯虚拟调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆