What is Override identifier?
Suppose we have a base class with one virtual function and releasing it to the clients. The user of this class can write a derived class. There he can override the base class virtual function. But accidentally he wrote a wrong parameter type like the following code sample. What will happen?
class Base
{
public:
virtual void Drive(int speed);
...
class Derived : public Base
{
public:
virtual void Drive(float speed);
...
The compiler will think that Drive(float speed) is a new virtual function in the derived class. But instead of having a new virtual function, the purpose of the derived class is to override base class function.
This issue can only caught by thorough code review or tedious debugging process. To help us in this kind of situation new version of C++ standards introduced an identifier named override
.
The compiler can detect this glitch and throws errors if we specify override
in the derived class overridden function. This is more meaningful and helps the human reader.
class Derived : public Base
{
public:
virtual void Drive(float speed) override;
...
This will cause following error.
error: ‘virtual void Derived::Drive(float)’ marked override, but does not override