Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

override — checking virtual function overriding

override requires a member function to override a virtual function from a base class. It catches declaration mistakes during compilation instead of allowing an inherited function to be hidden accidentally.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

return_type function_name(parameters) const override;
return_type function_name(parameters) const override { /* body */ }

override is a virtual function specifier (virt-specifier): it requires overriding a base-class virtual function. Failure to do so makes the program ill-formed and requires a diagnostic. An overriding function remains virtual without repeating virtual; override itself is not needed for virtual dispatch.

Write override on the declaration inside the class, after the function declarator and before ;, the body, or = 0. Qualifiers such as const, a reference qualifier, and a noexcept specification, when present, precede override. Do not repeat it on an out-of-class definition.

The name, parameter types, and function qualifiers must satisfy the overriding rules. In the example, removing const from Derived::value triggers an error because of override. Changing only the return type does not create an overload: the return type must be identical or satisfy covariance rules. override checks overriding, whereas final prohibits further overriding; both can be used together.

Strictly speaking, override is an identifier with special meaning in a particular context, not an unconditionally reserved keyword. In the example, the call through const Base& selects Derived::value. This entry targets C++23; the supplied source is an evolving draft, not a frozen C++23 publication. The repository compiler flag does not attest to the production runtime.

Examples

#include <iostream>

struct Base {
    virtual int value() const { return 1; }
    virtual ~Base() = default;
};

struct Derived : Base {
    int value() const override { return 2; }
};

int main() {
    Derived object;
    const Base& base = object;
    std::cout << base.value() << '\n';
}

Result

2

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources