Does final imply override?

后端 未结 5 1928
别跟我提以往
别跟我提以往 2021-01-31 01:28

As I understand it, the override keyword states that a given declaration implements a base virtual method, and the compilation should fail if there is

5条回答
  •  忘掉有多难
    2021-01-31 02:22

    The following code (with the final specifier) compiles. But compilation fails when final is replaced with override final. Thus override final conveys more information (and prevents compilation) than just final.

    class Base
    {
    public:
        virtual ~Base() {}
    };
    
    class Derived : public Base
    {
    public:
        virtual void foo() final
        {
            std::cout << "in Derived foo\n";
        }
    };
    

    Essentially, override final says this method cannot be overridden in any derived class and this method overrides a virtual method in a base class. final alone doesn't specify the base class overriding part.

提交回复
热议问题