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

虚基类构造函数执行顺序

时间:2015-04-18 12:46:06      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>

using namespace std;

class stream

{

public:

  stream(){cout<<"stream constructer"<<endl;

  ~stream(){cout<<"stream destructer"<<endl;

};

 

class iistream : virtual stream

{

public:

  iistream(){cout<<"iistream constructer"<<endl;

  ~iistream(){cout<<"iistream destructer"<<endl;

};

 

class oostream : virtual stream

{

public:

  oostream(){cout<<"oostream constructer"<<endl;

  ~oostream(){cout<<"oostream destructer"<<endl;

};

 

class iiostream:public iistream, public oostream

{

public:

  iiostream(){cout<<"iiostream constructer"<<endl;

  ~iiostream(){cout<<"iiostream destructer"<<endl;

};

 

int main()

{

  iiostream oo;

  return 0;

}

 

输出结果:

stream constructer

iistream constructer

oostream constructer

iiostream constructer

 

iiostream deconstructer

oostream deconstructer

iistream deconstructer

stream deconstructer

 

解释:

1. 虚基类构造函数会最先执行;

2. 派生类istream、ostream、iiostream都含有一个指向虚基类的vptr;

 

虚基类构造函数执行顺序

标签:

原文地址:http://www.cnblogs.com/hj-blog/p/4437049.html

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