C++中子类如何调用不同父类中的同名函数?

  当C++中多继承时,父类中可能含有同名函数,此时子类如何调用呢? 

  直接贴代码:

#include <iostream.h>
class B1{
public:
	void output();
};
class B2{
public:
	void output();
};
void B1::output(){
	cout<<"call the class B1"<<endl;
}
void B2::output(){
	cout<<"call the class B2"<<endl;
}
class A:public B1,public B2{
public:
	void show();
};
void A::show(){
	cout<<"call the class A"<<endl;
}
int main(){
	A a;
	a.B1::output();
	a.show();
	return 0;
}
如上例,不能直接用a.output(),而是用作用域运算符a.B1::output()显式指出所要调用的父类的函数。。。


当然子类中调用父类中被子类隐藏的函数或者覆盖的函数也可以用这种方式进行调用。。.见这篇blog第一个例子http://blog.csdn.net/lu597203933/article/details/8992687


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值