C++11中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:
#include "override.hpp"
#include <iostream>//
// reference: http://stackoverflow.com/questions/18198314/what-is-the-override-keyword-in-c-used-for
struct base_override {virtual void foo() = 0;
};struct derived_override : base_override {virtual void foo() override{std::cout << "__PRETTY_FUNCTION__" << std::endl;}
};int test_override1()
{base_override* override = new derived_override();override->foo();return 0;
}//
// reference: http://en.cppreference.com/w/cpp/language/override
struct A {virtual void foo();void bar();
};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
};//
// reference: https://msdn.microsoft.com/en-us/library/jj678987.aspx
class BaseClass
{virtual void funcA();virtual void funcB() const;virtual void funcC(int = 0);void funcD();
};class DerivedClass : public BaseClass
{virtual void funcA() override; // ok// virtual void funcB() override; // compiler error: DerivedClass::funcB() does not // override BaseClass::funcB() const// virtual void funcC(double = 0.0) override; // compiler error: // DerivedClass::funcC(double) does not // override BaseClass::funcC(int)// void funcD() override; // compiler error: DerivedClass::funcD() does not // override the non-virtual BaseClass::funcD()
};//
// reference: https://segmentfault.com/a/1190000003698366
struct B_ {virtual void f();virtual void g() const;virtual void h(char);void k(); // non-virtualvirtual void m() final;
};struct D_ : B_ {void f() override; // OK: 重寫 B::f()// void g() override; // error: 不同的函數聲明,不能重寫virtual void h(char); // 重寫 B::h( char ); 可能會有警告// void k() override; // error: B::k() 不是虛函數// virtual void m(); // error: m()在基類中聲明禁止重寫
};
GitHub:https://github.com/fengbingchun/Messy_Test
總結
以上是生活随笔為你收集整理的C++11中override的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++11中weak_ptr的使用
- 下一篇: Markdown语法简介