const function vs final function
#include <iostream>
class Base {
public:
virtual void normalFunction() {
std::cout << "Base: normalFunction" << std::endl;
}
virtual void constFunction() const {
std::cout << "Base: constFunction" << std::endl;
}
virtual void finalFunction() final {
std::cout << "Base: finalFunction" << std::endl;
}
virtual void constFinalFunction() const final {
std::cout << "Base: constFinalFunction" << std::endl;
}
};
class Derived : public Base {
public:
void normalFunction() override {
std::cout << "Derived: normalFunction" << std::endl;
}
void constFunction() const override {
std::cout << "Derived: constFunction" << std::endl;
}
// Uncommenting the following would cause a compilation error:
// void finalFunction() override {
// std::cout << "Derived: finalFunction" << std::endl;
// }
// Uncommenting the following would cause a compilation error:
// void constFinalFunction() const override {
// std::cout << "Derived: constFinalFunction" << std::endl;
// }
};
int main() {
Base base;
Derived derived;
Base* ptr = &derived;
ptr->normalFunction(); // Calls Derived::normalFunction
ptr->constFunction(); // Calls Derived::constFunction
ptr->finalFunction(); // Calls Base::finalFunction
ptr->constFinalFunction();// Calls Base::constFinalFunction
return 0;
}Last updated