码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-07-10 20:42:22      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:


#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

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

标签:

原文地址:http://www.cnblogs.com/xiumukediao/p/4637071.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!