【发布时间】:2026-01-26 10:55:02
【问题描述】:
虚析构声明后的覆盖标识符有什么特殊含义吗?
class Base
{
public:
virtual ~Base()
{}
virtual int Method() const
{}
};
class Derived : public Base
{
public:
virtual ~Derived() override
{}
virtual int Method() override // error: marked override, but does not override - missing const
{}
};
在虚拟方法上使用覆盖标识符作为检查很有用:当 Base 虚拟方法实际上没有被覆盖时,编译器将报告错误。
虚拟析构函数的覆盖是否也有任何意义/功能?
【问题讨论】:
-
编译器告诉你什么?
-
关于 ~Derived() 覆盖它什么也没说,它编译没有任何问题。我的意思是它是否有任何特殊含义。 Method() 覆盖当然是错误的,因为它缺少 const。 (我把它作为例子)
-
如果基地不是虚拟的,它does NOT compile。
标签: c++ c++11 overriding virtual-destructor