Namespaces
Variants
Views
Actions

override specifier

From cppreference.com
< cpp‎ | language
Revision as of 20:06, 31 May 2013 by P12bot (Talk | contribs)

 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function declaration
inline specifier
Exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
decltype (C++11)
auto (C++11)
alignas (C++11)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Implicit conversions - Explicit conversions
static_cast - dynamic_cast
const_cast - reinterpret_cast
Memory allocation
Classes
Class-specific function properties
Virtual function
override specifier (C++11)
final specifier (C++11)
Special member functions
Templates
Miscellaneous
 

Specifies that a virtual function overrides another virtual function.

Contents

Syntax

function_declaration override ;

Explanation

In a method declaration, override specifies that the function must be overriding a base class method.

override is an identifier with a special meaning when used after function declaration, otherwise it's not reserved.

Example

struct A
{
    virtual void foo();
    void bar();
};
 
struct B : A
{
    void foo() const override; // Error: Has a different signature from A::foo
    void foo() override; // OK: base class contains a virtual function with the same signature
    void bar() override; // Error: B::bar doesn't override because A::bar is not virtual
};

See also