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()显式指出所要调用的父类的函数。。。

 


 

posted @ 2013-05-29 19:30  javawebsoa  Views(492)  Comments(0Edit  收藏  举报