Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      override specifier(since C++11)

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      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
      constexpr(C++11)
      consteval(C++20)
      constinit(C++20)
      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
      Virtual function
      override specifier(C++11)  
      final specifier(C++11)
      Special member functions
      Templates
      Miscellaneous
       
       

      Specifies that avirtual function overrides another virtual function.

      Contents

      [edit]Syntax

      The identifieroverride, if used, appears immediately after thedeclarator in the syntax of a member function declaration or a member function definition inside a class definition.

      declaratorvirt-specifier-seq (optional)pure-specifier (optional) (1)
      declaratorvirt-specifier-seq (optional)function-body (2)
      1) In a member function declaration,override may appear invirt-specifier-seq immediately after the declarator, and before thepure-specifier, if used.
      2) In a member function definition inside a class definition,override may appear invirt-specifier-seq immediately after the declarator and just beforefunction-body.

      In both cases,virt-specifier-seq, if used, is eitheroverride orfinal, orfinal override oroverride final.

      [edit]Explanation

      In a member function declaration or definition,override specifier ensures that the function is virtual and is overriding a virtual function from a base class. The program is ill-formed (a compile-time error is generated) if this is not true.

      override is anidentifier with a special meaning when used after member function declarators; it is not a reservedkeyword otherwise.

      [edit]Keywords

      override

      [edit]Example

      Run this code
      #include <iostream> struct A{virtualvoid foo();void bar();virtual ~A();}; // member functions definitions of struct A:void A::foo(){std::cout<<"A::foo();\n";}A::~A(){std::cout<<"A::~A();\n";} struct B: A{//  void foo() const override; // Error: B::foo does not override A::foo// (signature mismatch)void foo() override;// OK: B::foo overrides A::foo//  void bar() override; // Error: A::bar is not virtual    ~B() override;// OK: `override` can also be applied to virtual// special member functions, e.g. destructorsvoid override();// OK, member function name, not a reserved keyword}; // member functions definitions of struct 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, invokes the member function `override()`int override{42};// OK, defines an integer variablestd::cout<<"override: "<< override<<'\n';}

      Output:

      B::foo();B::override();override: 42B::~B();A::~A();

      [edit]See also

      final specifier(C++11) declares that a method cannot be overridden or a class be derived from[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/override&oldid=176068"

      [8]ページ先頭

      ©2009-2025 Movatter.jp