首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

2012-04-01 
关于虚继承和虚函数的问题#include iostreamusingnamespacestdclassa{}classb{intb1public:b(inta){b

关于虚继承和虚函数的问题
#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

热点排行