Search code examples
c++c++11overridingvirtualvirtual-functions

Difference between redefining and overriding a function


Suppose I have class A with a virtual function F():

class A
{
    virtual void F()
    {
        // Do something
    };
};

And I have another class B which inherits A and redefines F():

class B : A
{
    void F()
    {
        // Do something
    };
};

And a different class C which also inherits A but overrides F():

class C : A
{
    void F() override
    {
        // Do something
    };
};

What is the difference between F() in classes B and C?


Solution

  • Both B::f() and C::f() are overrides and they are exactly the same.

    override is essentially a compile-time advisory term that will cause a compilation error if the function does not override one in a base class.

    This can help program stability: if the name and parameter types to A::f() are changed, then a compile error will result.