HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppCritical

Is the 'override' keyword just a check for a overridden virtual method?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
checkoverridetheforvirtualmethodjustkeywordoverridden

Problem

As far as I understand, the introduction of override keyword in C++11 is nothing more than a check to make sure that the function being implemented is the overrideing of a virtual function in the base class.

Is that it?

Solution

That's indeed the idea. The point is that you are explicit about what you mean, so that an otherwise silent error can be diagnosed:

struct Base
{
    virtual int foo() const;
};

struct Derived : Base
{
    virtual int foo()   // whoops!
    {
       // ...
    }
};


The above code compiles, but is not what you may have meant (note the missing const). If you said instead, virtual int foo() override, then you would get a compiler error that your function is not in fact overriding anything.

Code Snippets

struct Base
{
    virtual int foo() const;
};

struct Derived : Base
{
    virtual int foo()   // whoops!
    {
       // ...
    }
};

Context

Stack Overflow Q#13880205, score: 316

Revisions (0)

No revisions yet.