Final virtual functions

suggest change

C++11 introduced final specifier which forbids method overriding if appeared in method signature:

class Base {
public:
    virtual void foo() {
        std::cout << "Base::Foo\n";
    }
};

class Derived1 : public Base {
public:
    // Overriding Base::foo
    void foo() final {
        std::cout << "Derived1::Foo\n";
    }
};

class Derived2 : public Derived1 {
public:
    // Compilation error: cannot override final method
    virtual void foo() {
        std::cout << "Derived2::Foo\n";
    }
};

The specifier final can only be used with `virtual’ member function and can’t be applied to non-virtual member functions

Like final, there is also an specifier caller ‘override’ which prevent overriding of virtual functions in the derived class.

The specifiers override and final may be combined together to have desired effect:

class Derived1 : public Base {
public:
    void foo() final override {
        std::cout << "Derived1::Foo\n";
    }
};

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Virtual member functions:
* Syntax
* Final virtual functions

Table Of Contents
8 Arrays
11 Loops
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
70 Virtual member functions
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype