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-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

在语言,没有直接的关键字或特定的语法来实现函数的重写(override)。C是一种过程式编程语言,并没有提供类或继承的概念。因此,C的函数只能通过函数指针来实现类似于重写的行为。 在C,函数指针可以用于实现多态性(polymorphism),即在运行时动态地确定要调用的函数。具体来说,可以定义一个函数指针类型,并使用该类型的指针变量来存储不同函数的地址。通过更改该指针变量所指向的函数地址,可以在运行时切换要调用的函数,实现类似于重写的效果。 下面是一个简单的示例,说明如何使用函数指针来实现重写的效果: ```c #include <stdio.h> // 基类 typedef struct { void (*print)(void); } Base; // 派生类1 typedef struct { Base base; } Derived1; void derived1_print(void) { printf("Derived1\n"); } // 派生类2 typedef struct { Base base; } Derived2; void derived2_print(void) { printf("Derived2\n"); } int main() { Derived1 derived1; derived1.base.print = derived1_print; derived1.base.print(); // 输出:Derived1 Derived2 derived2; derived2.base.print = derived2_print; derived2.base.print(); // 输出:Derived2 return 0; } ``` 在这个示例使用了结构体来模拟类的概念。通过在基类定义一个函数指针成员(`print`),派生类可以根据需要重写该函数。通过更改函数指针的值,可以在运行时调用不同的函数。 需要注意的是,这种方式只能模拟类似于重写的效果,实际上并没有真正的重写功能。在C++等面向对象的语言,函数重写是通过虚函数(virtual function)和继承来实现的。如果需要真正的重写功能,建议考虑使用C++或其他支持面向对象编程的语言。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值