c++ override

override是C++11中的一个继承控制保留字,放在派生类成员函数参数列表后面,用来修饰函数。override确保在派生类中声明的重载函数跟基类的虚函数有相同的签名。在父类中有一个与之对应(形参、函数名、返回值都一致)的虚函数,override表示要重写父类的虚函数,一旦函数后面加了override,编译器就会检查父类中是否有和子类中签名匹配的函数,如果没有编译器会报错。

#include "stdafx.h"

#include <iostream>

using namespace std;



class Parent 

{

public:

virtual void Func();

void Func_B();

virtual void Func_C() final{ }

};



void Parent::Func()

{

cout<<"call the function of Parent"<<endl;

}



class Child  : public Parent 

{

public:

void Func() override;//基类声明的虚函数,在派生类中也是虚函数,即使不再使用virtual关键字



/*************************************************************************

void Func_A() override;

父类中没有此方法,添加override编译会报如下错错误:

error C3668: “Child::Func_A”: 包含重写说明符“override”的方法没有重写任何基类方法

*************************************************************************/



/*************************************************************************

void Func_B() override { }

Func_B在父类中不是虚函数,添加override编译会报如下错错误:

error C3668: “Child::Func_B”: 包含重写说明符“override”的方法没有重写任何基类方法

*************************************************************************/



/*

void Func_C() override { }

Func_C在父类中被final修饰,禁止在派生类中被重写

error: 
Func_C在基类中声明禁止重写 

*/

};



void Child::Func()

{

cout<<"implement the function of Parent"<<endl;

}



int _tmain(int argc, _TCHAR* argv[])

{

Parent objParent;

Child objChild;

return 0;

}

http://blog.csdn.net/jolin678/article/details/63695023

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值