标签:
#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