当前位置:   article > 正文

C++11中override的使用_c++ override

c++ override

override是C++11中的一个继承控制关键字。override确保在派生类中声明的重载函数跟基类的虚函数有相同的声明

override明确地表示一个函数是对基类中一个虚函数的重载。更重要的是,它会检查基类虚函数和派生类中重载函数的签名不匹配问题。如果签名不匹配,编译器会发出错误信息。

override表示函数应当重写基类中的虚函数(用于派生类的虚函数中)

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

The override special identifier means that the compiler will check the base class(es) to see if there is a virtual function with this exact signature. And if there is not,the compiler will indicate an error.

Declaring a method as "override" means that that method is intended to rewrite a(virtual) method on the base class. The overriding method must have same signature (at least for the input parameters) as the method it intends to rewrite.

Adding"override" clearly disambiguates this: through this, one is telling the compiler that three things are expecting:

(1)、there is a method with the same name in the superclass.

(2)、this method in the superclass is declared as "virtual" (that means, intended to be rewritten).

(3)、the method in the superclass has the same (input*) signature as the method in the subclass(the rewriting method).

If any of these is false, then an error is signaled.

The override keyword serves two purposes:

(1)、It shows the reader of the code that "this is a virtual method, that is overriding a virtual method of the base class."

(2)、The compiler also knows that it's an override, so it can "check" that you are not altering/adding new methods that you think are overrides.。

下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:

  1. #include "override.hpp"
  2. #include <iostream>
  3. //
  4. // reference: http://stackoverflow.com/questions/18198314/what-is-the-override-keyword-in-c-used-for
  5. struct base_override {
  6. virtual void foo() = 0;
  7. };
  8. struct derived_override : base_override {
  9. virtual void foo() override
  10. {
  11. std::cout << "__PRETTY_FUNCTION__" << std::endl;
  12. }
  13. };
  14. int test_override1()
  15. {
  16. base_override* override = new derived_override();
  17. override->foo();
  18. return 0;
  19. }
  20. //
  21. // reference: http://en.cppreference.com/w/cpp/language/override
  22. struct A {
  23. virtual void foo();
  24. void bar();
  25. };
  26. struct B : A {
  27. // void foo() const override; // Error: B::foo does not override A::foo (signature mismatch)
  28. void foo() override; // OK: B::foo overrides A::foo
  29. // void bar() override; // Error: A::bar is not virtual
  30. };
  31. //
  32. // reference: https://msdn.microsoft.com/en-us/library/jj678987.aspx
  33. class BaseClass
  34. {
  35. virtual void funcA();
  36. virtual void funcB() const;
  37. virtual void funcC(int = 0);
  38. void funcD();
  39. };
  40. class DerivedClass : public BaseClass
  41. {
  42. virtual void funcA() override; // ok
  43. // virtual void funcB() override; // compiler error: DerivedClass::funcB() does not
  44. // override BaseClass::funcB() const
  45. // virtual void funcC(double = 0.0) override; // compiler error:
  46. // DerivedClass::funcC(double) does not
  47. // override BaseClass::funcC(int)
  48. // void funcD() override; // compiler error: DerivedClass::funcD() does not
  49. // override the non-virtual BaseClass::funcD()
  50. };
  51. //
  52. // reference: https://segmentfault.com/a/1190000003698366
  53. struct B_ {
  54. virtual void f();
  55. virtual void g() const;
  56. virtual void h(char);
  57. void k(); // non-virtual
  58. virtual void m() final;
  59. };
  60. struct D_ : B_ {
  61. void f() override; // OK: 重写 B::f()
  62. // void g() override; // error: 不同的函数声明,不能重写
  63. virtual void h(char); // 重写 B::h( char ); 可能会有警告
  64. // void k() override; // error: B::k() 不是虚函数
  65. // virtual void m(); // error: m()在基类中声明禁止重写
  66. };

GitHubhttps://github.com/fengbingchun/Messy_Test

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/950571
推荐阅读
  

闽ICP备14008679号