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

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

2013-05-29 18:19 267 查看
当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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: