C++中子類如何呼叫不同父類中的同名函式?
阿新 • • 發佈:2019-02-12
當C++中多繼承時,父類中可能含有同名函式,此時子類如何呼叫呢?
直接貼程式碼:
如上例,不能直接用a.output(),而是用作用域運算子a.B1::output()顯式指出所要呼叫的父類的函式。。。#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; }
當然子類中呼叫父類中被子類隱藏的函式或者覆蓋的函式也可以用這種方式進行呼叫。。.見這篇blog第一個例子http://blog.csdn.net/lu597203933/article/details/8992687