在C ++中的纯虚拟析构函数 [英] Pure virtual destructor in C++

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

问题描述

写错了:

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

至少在MSVC中编译...它会在运行时崩溃吗?

At least that compiles in MSVC... Will it crash at run time?

推荐答案

是的。你还需要实现析构函数:

Yes. You also need to implement the destructor:

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

inline A::~A() { }

由于这是一个向下的投票,我应该澄清:如果你从A派生任何东西,然后尝试删除或销毁它, A 的析构函数最终将被调用。由于它是纯粹的,没有实现,未定义的行为将随之而来。在一个流行的平台上,这将调用purecall处理程序和崩溃。

And since this got a down vote, I should clarify: If you derive anything from A and then try to delete or destroy it, A's destructor will eventually be called. Since it is pure and doesn't have an implementation, undefined behavior will ensue. On one popular platform, that will invoke the purecall handler and crash.

编辑:修复声明更符合,编译与http://www.comeaucomputing.com/tryitout/

fixing the declaration to be more conformant, compiled with http://www.comeaucomputing.com/tryitout/

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

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