Pure virtual destructor in C++(C++中的纯虚析构函数)
本文介绍了C++中的纯虚析构函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Is it wrong to write:
class A {
public:
virtual ~A() = 0;
};
for an abstract base class?
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() { }
should suffice.
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.
Edit: fixing the declaration to be more conformant, compiled with http://www.comeaucomputing.com/tryitout/
这篇关于C++中的纯虚析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:C++中的纯虚析构函数
基础教程推荐
猜你喜欢
- 初始化变量和赋值运算符 1970-01-01
- 使用scanf()读取字符串 1970-01-01
- 明确指定任何或所有枚举数的整数值 1970-01-01
- end() 能否成为 stl 容器的昂贵操作 2022-10-23
- C语言访问数组元素 1970-01-01
- C++ #define 1970-01-01
- C++按值调用 1970-01-01
- C++输入/输出运算符重载 1970-01-01
- C++定义类对象 1970-01-01
- 分别使用%o和%x以八进制或十六进制格式显示整 1970-01-01