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()在基类中声明禁止重写
};
GitHub:https://github.com/fengbingchun/Messy_Test
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()在基类中声明禁止重写
};
GitHub:https://github.com/fengbingchun/Messy_Test
相关文章推荐
- C++11新特性(二)override, final 简单使用
- C++11中override的使用
- abstract 能和override 一起使用[C#]
- 在ASP.NET页面中推荐使用覆写(Override)而不是事件处理(Event Handler)
- c#编程之使用 Override 和 New 关键字进行版本控制
- c#中 接口成员为什么不能使用关键字static virtul override abstract
- 了解何时使用 Override 和 New 关键字(C# 编程指南)
- new、abstract、virtual、override,sealed关键字区别和使用代码示例
- virtual/override的使用
- C#中virtual 与 override 的使用
- C#下 子类继承父类的虚方法后 使用override重写父类方法 和 不使用override重写有什么区别?
- 在ASP.NET页面中推荐使用覆写(Override)而不是事件处理(Event Handler)
- 了解何时使用 Override 和 New 关键字
- jdk1.5为什么需要使用@Override标记,其作用是什么
- 了解何时使用 Override 和 New 关键字(C# 编程指南)
- 了解何时使用 Override 和 New 关键字(C# 编程指南)
- override和new使用指南
- Virtual、Override和New关键字的使用
- new、abstract、virtual、override,sealed关键字区别和使用代码示例
- 使用 Override 和 New 关键字进行版本控制(C# 编程指南)