Override identifier after destructor in C++11

后端 未结 2 1763
不思量自难忘°
不思量自难忘° 2021-01-30 06:50

Does the override identifier after virtual destructor declaration have any special meaning?

class Base
{
public:
    virtua         


        
相关标签:
2条回答
  • 2021-01-30 07:08

    It is not override that has special meaning, but the destructor itself:

    10.3 Virtual Functions

    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/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 
    }; 
    

    —end example ]

    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.

    0 讨论(0)
  • 2021-01-30 07:14

    Yes. If the base destructor is not virtual then the override marking will cause the program to not compile:

    class Base
    {
    public:
        ~Base()
        {}
    };
    
    class Derived : public Base
    {
    public:
        virtual ~Derived() override //error: '~Derived' marked 'override' but does
                                    //        not override any member functions
        {}
    };
    
    0 讨论(0)
提交回复
热议问题