This article will talk about the override keyword in C++. It can be explained in two sentences, but for completeness, let’s start with virtual functions. In C++, virtual functions are one of the most common mechanisms for implementing polymorphism. Let’s review the simplest example:

class Base/ / base class {
public:
    virtual void f(a){cout << "Base::f()" << endl;}
};
​
class Derived1 : public Base // Derive class 1
{
    virtual void f(a){cout << "Derived1::f()" << endl;}
};
​
class Derived2 : public Base // Derived class 2
{
    virtual void f(a){cout << "Derived2::f()" << endl;}
};
Copy the code

(Please ignore the layout style for the above code)

The above is a minimalist example of a Base class and its derived subclasses that have a common virtual function f() that the derived classes override.

We want to call the derived version of the function f() via a base class pointer or a base class reference to achieve the so-called polymorphism, as shown in the following code:

Base *b;
    
b  = new Derived1;
b->f(a);/ / print "Derived1: : f ()"
    
b = new Derived2;
b->f(a);/ / print "Derived2: : f ()"
Copy the code

However, as an ordinary virtual function f(), it doesn’t actually require us to override it. If you don’t override it in a derived class, the derived class will naturally use the alternate version provided by the base class.

Therein lies the danger. Humans are a species with many faults, one of which is self-righteousness and carelessness, so the following code could very well have been written by a fellow countryman:

class Derived3 : public Base // Derived class 3
{
    // Note: the following functions take arguments
    // We thought we were copying the base virtual function, but we didn't
    virtual void f(int){cout << "Derived3::f()" << endl;}
};
Copy the code

It is clear that this sibling has the same intention as the previous two derived classes: Derived3 and overwriting the virtual function f(). Unfortunately, if the compatriot executes the following code rashly, it will bring disastrous consequences:

Base *b;
b = new Derived3;
b->f(a);Derived2=3::f()
        // Actually prints "Base::f()"!
Copy the code

If that’s not disastrous enough, think of the function f() as the takeoff guidance program for a commercial airplane.

Now the problem is obvious:

Because of human stupidity, it is quite possible to make an error when copying a virtual function of a derived class (such as void f()), but actually writing another function (such as void f(int)). Unfortunately, C++ syntax does not prevent this foolishness. It’ll think we’re doing it for some mysterious reason.

Then, the stupid human executes the program, pawn.

Solutions:

To eliminate the profound misunderstanding between humans and compilers, that is, when we want to duplicate virtual functions, we also explicitly tell the compiler that we want to do so, so that it does not have any misunderstanding. How do I tell it? Pedalling deng Deng suffocated for a long time the protagonist finally appeared bird:

class Derived3 : public Base // Derived class 3
{
    / / note:
    // Override explicitly tells the compiler that I want to override the virtual function
    Void f(int) = void f(int); Oh, also!
    virtual void f(int) override 
    {cout << "Derived3::f()" << endl;}
};
Copy the code

the End.