Using override with virtual in C++ 11

suggest change

The specifier override has a special meaning in C++11 onwards, if appended at the end of function signature. This signifies that a function is

There is no run time significance of this specifier as is mainly meant as an indication for compilers

The example below will demonstrate the change in behaviour with our without using override.

Without override:

#include <iostream>

struct X {
    virtual void f() { std::cout << "X::f()\n"; }
};

struct Y : X {
	// Y::f() will not override X::f() because it has a different signature, 
	// but the compiler will accept the code (and silently ignore Y::f()). 
	virtual void f(int a) { std::cout << a << “\n; }
};

With override:

#include <iostream>

struct X {
    virtual void f() { std::cout << "X::f()\n"; }
};

struct Y : X {
	// The compiler will alert you to the fact that Y::f() does not 
	// actually override anything. 
	virtual void f(int a) override { std::cout << a << “\n; }
};

Note that override is not a keyword, but a special identifier which only may appear in function signatures. In all other contexts override still may be used as an identifier:

void foo() {
	int override = 1; // OK. 
	int virtual = 2; // Compilation error: keywords can’t be used as identifiers.
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Virtual member functions:
* Syntax
* Using override with virtual in C++ 11

Table Of Contents
8 Arrays
11 Loops
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
70 Virtual member functions
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype