虚函数重名
原文 基类有同名虚函数怎么实现
#include <iostream> using namespace std; class iBaseA { public: virtual void print() = 0; }; class iBaseB { public: virtual void print() = 0; }; class cBaseA : public iBaseA { public: virtual void print(){return printBaseA();}; private: virtual void printBaseA() = 0; }; class cBaseB : public iBaseB { public: virtual void print(){return printBaseB();}; private: virtual void printBaseB() = 0; }; class cBase : public cBaseA,public cBaseB { private: virtual void printBaseA() {cout << "BaseA" << endl;}; virtual void printBaseB(){cout << "BaseB" << endl;}; }; int main() { cBase oBase; iBaseA* pBaseA = dynamic_cast<iBaseA*>(&oBase); iBaseB* pBaseB = dynamic_cast<iBaseB*>(&oBase); pBaseA->print(); pBaseB->print(); return 0; }