虚函数重名

原文 基类有同名虚函数怎么实现

 

#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;
}
View Code

 

 

posted on 2013-11-27 14:22  空娴  阅读(226)  评论(0编辑  收藏  举报

导航