您的位置:首页 > 编程语言 > C语言/C++

C++11中override的使用

2016-08-24 21:38 387 查看
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.
         override是C++11中的一个继承控制关键字。override确保在派生类中声明的重载函数跟基类的虚函数有相同的声明。
override明确地表示一个函数是对基类中一个虚函数的重载。更重要的是,它会检查基类虚函数和派生类中重载函数的签名不匹配问题。如果签名不匹配,编译器会发出错误信息。
override表示函数应当重写基类中的虚函数(用于派生类的虚函数中)。
下面是从其他文章中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-virtual
virtual 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()在基类中声明禁止重写
};
GitHubhttps://github.com/fengbingchun/Messy_Test
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: