override
specifier
(since C++11)
| General topics | ||||||||||||||||
| Flow control | ||||||||||||||||
| Conditional execution statements | ||||||||||||||||
| Iteration statements (loops) | ||||||||||||||||
|
||||||||||||||||
| Jump statements | ||||||||||||||||
| Functions | ||||||||||||||||
| Function declaration | ||||||||||||||||
| Lambda function expression | ||||||||||||||||
inline
specifier
|
||||||||||||||||
| Dynamic exception specifications ( until C++17* ) | ||||||||||||||||
noexcept
specifier
(C++11)
|
||||||||||||||||
| Exceptions | ||||||||||||||||
| Namespaces | ||||||||||||||||
| Types | ||||||||||||||||
| Specifiers | ||||||||||||||||
|
||||||||||||||||
| 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 | ||||||||||||||||
| Memory allocation | ||||||||||||||||
| Classes | ||||||||||||||||
| Class-specific function properties | ||||||||||||||||
|
||||||||||||||||
| Special member functions | ||||||||||||||||
|
||||||||||||||||
| Templates | ||||||||||||||||
| Miscellaneous | ||||||||||||||||
| General | ||||
| Overview | ||||
class
/
struct
types
|
||||
union
types
|
||||
| Injected-class-name | ||||
| Class property specifiers (C++26) | ||||
| Members | ||||
| Data members | ||||
| Static members | ||||
The
this
pointer
|
||||
| Nested classes | ||||
| Member templates | ||||
| Bit-fields | ||||
using
-declarations
|
||||
| Member functions | ||||
| Member access specifiers | ||||
| Constructors and member initializer lists | ||||
| Default member initializer (C++11) | ||||
friend
specifier
|
||||
explicit
specifier
|
||||
| Converting constructor | ||||
| Special member functions | ||||
| Default constructor | ||||
| Copy constructor | ||||
| Move constructor (C++11) | ||||
| Copy assignment operator | ||||
| Move assignment operator (C++11) | ||||
| Destructor | ||||
| Inheritance | ||||
| Base and derived classes | ||||
| Empty base optimization (EBO) | ||||
| Virtual member functions | ||||
| Pure virtual functions and abstract classes | ||||
override
specifier
(C++11)
|
||||
final
specifier
(C++11)
|
仮想関数が別の仮想関数を override することを指定します。
目次 |
構文
識別子
override
は、使用される場合、クラス定義内のメンバ関数宣言またはメンバ関数定義の構文において、
declarator
の直後に現れます。
| declarator virt-specifier-seq (optional) pure-specifier (optional) | (1) | ||||||||
| declarator virt-specifier-seq (optional) function-body | (2) | ||||||||
override
は、宣言子の直後、
virt-specifier-seq
内に記述でき、使用される場合には
pure-specifier
の前に記述することができます。
override
は宣言子の直後、
function-body
の直前の
virt-specifier-seq
内に記述できます。
どちらの場合でも、
virt-specifier-seq
が使用される場合、それは
override
または
final
、あるいは
final override
または
override final
のいずれかです。
説明
メンバ関数の宣言または定義において、 override 指定子は、その関数が仮想関数であり、基底クラスからの仮想関数をオーバーライドしていることを保証します。これが真でない場合、プログラムは不適格となります(コンパイル時エラーが発生します)。
override
は、メンバー関数の宣言子の後に使用された場合、
特別な意味を持つ識別子
であり、それ以外の場合は予約された
キーワード
ではありません。
キーワード
例
#include <iostream> struct A { virtual void foo(); void bar(); virtual ~A(); }; // 構造体Aのメンバ関数定義: void A::foo() { std::cout << "A::foo();\n"; } A::~A() { std::cout << "A::~A();\n"; } struct B : A { // void foo() const override; // エラー: B::fooはA::fooをオーバーライドしない // (シグネチャ不一致) void foo() override; // OK: B::fooはA::fooをオーバーライドする // void bar() override; // エラー: A::barは仮想関数ではない ~B() override; // OK: `override`は仮想特殊メンバ関数 // (例: デストラクタ)にも適用可能 void override(); // OK, メンバ関数名であり予約語ではない }; // 構造体Bのメンバ関数定義: void B::foo() { std::cout << "B::foo();\n"; } B::~B() { std::cout << "B::~B();\n"; } void B::override() { std::cout << "B::override();\n"; } int main() { B b; b.foo(); b.override(); // OK, メンバ関数`override()`を呼び出す int override{42}; // OK, 整数変数を定義 std::cout << "override: " << override << '\n'; }
出力:
B::foo(); B::override(); override: 42 B::~B(); A::~A();
関連項目
final
指定子
(C++11)
|
メソッドがオーバーライドできないこと、またはクラスが派生できないことを宣言する |