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