c++ - 在 C++11 中的析构函数之后覆盖标识符

标签 c++ c++11 overriding virtual-destructor

虚析构声明后的覆盖标识符有什么特殊含义吗?

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 虚拟方法实际上没有被覆盖时,编译器将报告错误。

虚拟析构函数的覆盖是否也有任何意义/功能?

最佳答案

是的。如果基本析构函数不是虚拟的,那么 override 标记将导致程序无法编译:

class Base
{
public:
    ~Base()
    {}
};

class Derived : public Base
{
public:
    virtual ~Derived() override //error: '~Derived' marked 'override' but does
                                //        not override any member functions
    {}
};

关于c++ - 在 C++11 中的析构函数之后覆盖标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17923370/

相关文章:

reactjs - 从 materialui 组件中删除/覆盖默认样式

c++ - Gobby 0.4.12安装报错

c++ - 在 C++ 中连接到 MySQL

c++ - C++标准是否保证cin、cout等先创建后销毁?

ruby-on-rails - 覆盖 Rails 翻译助手

java - java中的初始化顺序,继承和重写字段

c++ - 使用 SWIG 包装 Lua 对象以在 C++ 中使用

c++ - 将代码从 C 转换为 C++ : typedef void and <unresolved overloaded function type>

c++ - 通过引用传递原始类型会适得其反吗?

c++ - 格式错误的调用的零长度可变参数扩展