当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