多继承时,多个基类中存在型别相同的虚函数,该怎么做?


#include "stdafx.h" #include<iostream> using namespace std; class A{ public: virtual void show(){ cout<<"in classA"<<endl; } virtual ~A(){} }; class B{ public: virtual void show(){ cout<<"in classB"<<endl; } virtual ~B(){} }; class MidA:public A{ public: virtual void realshowA() = 0; virtual void show(){ realshowA(); } // virtual ~MidA(){} }; class MidB:public B{ public: virtual void realshowB() = 0; virtual void show(){ realshowB(); } // virtual ~MidB(){} }; class Derive :public MidA,public MidB{ public: void realshowA(){ cout<<"realshow in classA"<<endl; } void realshowB(){ cout<<"realshow in classB"<<endl; } // virtual ~Derive(){} }; int main(int argc,char * argv){ A *pa= new Derive; B *pb= new Derive; pa->show(); pb->show(); delete pa; delete pb; return 0; }

 只要基类声明为析构函数派生类的所有析构函数自然也为虚函数。参考effective C++ 条款07

posted @ 2015-07-10 19:21  朽木可雕否  阅读(1091)  评论(0编辑  收藏  举报