C++Builder Learn How To Use C++ Explicit Virtual Overrides In Windows Development

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,203
Credits
4,034
Learn How To Use C++ Explicit Virtual Overrides In Windows Development
January 20, 2021 by Pabitra Dash

Regarding virtual overrides, C++11 tends to tighten the rules, so as to detect some problems that often arise. In order to achieve this goal C++11 introduces two new contextual keywords:
  • final specifies that a method cannot be overridden or a class cannot be derived. When applied to a member function, the identifier final appears immediately after the declarator in the syntax of a member function declaration or a member function definition inside a class definition. When used in a virtual function declaration or definition, final specifier ensures that the function is virtual and specifies that it may not be overridden by derived classes. The program is ill-formed (a compile-time error is generated) otherwise.
  • override specifies that a method overrides a virtual method declared in one of its parent classes. The identifier override, if used, appears immediately after the declarator in the syntax of a member function declaration or a member function definition inside a class definition. 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.
Explicit virtual overrides example:

Код:
struct base {
        virtual void a();
        void b();
        virtual void c() final;
        virtual void d();
};
 
struct derived : base {
        void a() override; // correct
        void b() override; // error, override can only be used for virtual functions
        void c() override; // error, cannot override a function marked as final
        int d() override; // error, different return type
};

Для просмотра ссылки Войди или Зарегистрируйся