For functions in derived classes to actually override their base-class counterparts, some things have to be taken into account:
- The base-class method has to be declared “override"
- The function names must be identical (except for destructors)
- The parameter types of the base and derived functions must be identical (not only compatible).
- The const-ness of the base and derived functions must be identical.
- The return types and exception specifications of the base and derived functions must be compatible.
- The reference-qualifieres of the base and derived functions must be identical.
Thus in this case, there is not a single method in the derived class that overrides its base-class counterpart:
class Base
{
public:
virtual void f1() const;
virtual void f2(int x);
virtual void f3() &;
void f4() const;
};
class Derived : public Base
{
public:
virtual void f1();
virtual void f2(unsigned int x);
virtual void f3() &&;
void f4() const;
};
- wrong const-nesss
- non-identical types
- wrong reference-qualifiers
- not virtual in base class
By using override after the functions, all of those errors would have shown!
class Base { public: virtual void f1() const; virtual void f2(int x); virtual void f3() &; void f4() const; }; class Derived : public Base { public: virtual void f1() override; // 'f1' marked 'override' but does not override any member functions virtual void f2(unsigned int x) override; // 'f2' marked 'override' but does not override any member functions virtual void f3() && override; // 'f3' marked 'override' but does not override any member functions void f4() const override; // Only virtual member functinos can be marked 'override'
};
Thus, use override!