为什么纯虚拟析构函数的实现是空的?应该是内联的吗? [英] Why must a pure virtual destructor's implementation be empty? And should it be inline?

查看:390
本文介绍了为什么纯虚拟析构函数的实现是空的?应该是内联的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其他线程中读到,当你实现一个纯虚拟析构函数(是的,它可以有一个实现),它必须是空的,应该(?)内联。应该是空的吗?如果是,为什么?应该是内联吗?如果是,为什么?

I read in other threads that when you implement a pure virtual destructor (yes it can have an implementation) it must be empty, and should (?) be inline. Should it be empty? If so, why? Should it be inline? If so, why?

编辑:
这是一个纯虚拟解析器如何具有实现:

This is how a pure virtual descructor can have an implementation:

class A{
    virtual ~A() = 0;
}

inline A::~A(){
    //implementation
}


推荐答案

纯虚拟析构器必须具有实现(假设您至少有一个具体的派生类)。

A pure virtual destructor must have an implementation (assuming you have at least one concrete derived class).

没有规则说明纯虚拟析构函数必须具有空主体。我也不知道任何理由,除非同样的原因大多数析构函数应该有一个空体。

There is no rule that a pure virtual destructor must have empty body. Nor do I know of any reason that it should, except the same reasons most destructors should have an empty body.

纯虚拟析构函数可以内联或非内联。我希望每个的好处取决于基类和具有非平凡析构函数的非静态成员的数量。

A pure virtual destructor can be inline or non-inline. I would expect the benefits of each to depend on the number of base classes and non-static members with non-trivial destructors.

另一个catch,但是:在某些流行编译器,如果析构函数是为类定义的唯一虚方法,那么使它非内联,以帮助实现处理它的多态性魔法是一个好主意。

One other catch, though: on certain popular compilers, if the destructor is the only virtual method defined for the class, it is a good idea to make it non-inline, to help the implementation deal with its polymorphism magic.

这篇关于为什么纯虚拟析构函数的实现是空的?应该是内联的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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