在C ++ 11中的析构函数之后覆盖标识符 [英] Override identifier after destructor in C++11
问题描述
在虚拟析构函数声明之后的覆盖标识符是否有特殊含义?
Does the override identifier after virtual destructor declaration have any special meaning?
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
{}
};
在虚拟方法上使用覆盖标识符非常有用,可作为检查:当实际上不覆盖基本虚拟方法时,编译器将报告错误.
Using override identifier on virtual method is useful as check: compiler will report error when the Base virtual method is actualy not overriden.
在虚拟析构函数上进行重写是否也具有任何意义/功能?
Does override on virtual destructor has any meaning/function too?
推荐答案
不是override
具有特殊含义,而是析构函数本身:
It is not override
that has special meaning, but the destructor itself:
6/即使不继承析构函数,派生函数中的析构函数 class覆盖声明为virtual的基类析构函数;见12.4和 12.5.
6/Even though destructors are not inherited, a destructor in a derived class overrides a base class destructor declared virtual; see 12.4 and 12.5.
如果将其与上一条结合使用:
If you take this in conjunction with the previous clause:
5/如果用virt-specifier覆盖标记了虚拟功能,并且 不覆盖基类的成员函数,该程序是 格式不正确. [示例:
5/If a virtual function is marked with the virt-specifier override and does not override a member function of a base class, the program is ill-formed. [ Example:
struct B {
virtual void f(int);
};
struct D : B
{
void f(long) override; // error: wrong signature overriding B::f
void f(int) override; // OK
};
-结束示例]
您会看到,如果将析构函数标记为override
,但是基类没有virtual
析构函数,则程序格式错误.
you can see that if a destructor is marked override
but the base class does not have a virtual
destructor, the program is ill-formed.
这篇关于在C ++ 11中的析构函数之后覆盖标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!