读书人

关于虚继承和虚函数的有关问题

发布时间: 2012-04-01 17:23:46 作者: rapoo

关于虚继承和虚函数的问题
#include <iostream>
using namespace std;
class a{};
class b{
int b1;
public:
b(int a){b1=a;cout < < "make b " < <endl;}
virtual void show(){cout < <b1 < < " show b " < <endl;}
};
class c:virtual public b{
int c1;
public:
c(int a,int b):b(a){c1=b;cout < < "make c " < <endl;}
void show(){cout < < "show c " < <endl; }
};
class d:public b{
int d1;
public:
d(int a,int b):b(a){d1=b;cout < < "make d " < <endl;}
void show(){cout < < "show d " < <endl;}
};
class e:public c,public d{
//int e;
public:
e(int a,int b1,int c,int i,int l):c(a,b1),d(c,i),b(l){cout < < "make e " < <endl;}
void show(){/*b:: show();*/cout < < "--show e " < <endl;}
};
void test(b s){s.show();}
void main()
{
cout < <sizeof(a) < < ", " < <sizeof(b) < < ", " < <sizeof(c) < < ", " < <sizeof(d) < < ", " < <sizeof(e) < <endl;
}







如果有谁可以看懂为什么输出的是1,8,20,12,32的话,就说一下,比如那20中都有什么东西啊,还有那12,那32更是一塌糊涂啦

[解决办法]
连《深度探索C++对象模型》都不肯去探索虚继承,楼主就省点心吧。
大概答案知道:vptr和vbtr

读书人网 >C++

热点推荐