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

多继承的构造顺序

时间:2014-10-02 00:39:21      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   sp   div   2014   

构造函数按一下顺序被调用:

1.按虚拟基类被继承的顺序

2.按非虚拟基类被继承的顺序

3.按成员函数被声明的顺序

4.类自己的构造函数

 

#include<stdio.h>
#include<iostream>
#include<assert.h>
using namespace std;

class OBJ1
{
public:
    OBJ1(){ cout<<"OBJ1"<<endl; }
};

class OBJ2
{
public:
    OBJ2(){ cout<<"OBJ2"<<endl; }
};

class Base1
{
public:
    Base1(){ cout<<"Base1"<<endl; }
};

class Base2:public Base1
{
public:
    Base2(){ cout<<"Base2"<<endl; }
};

class Base3
{
public:
    Base3(){ cout<<"Base3"<<endl; }
};

class Base4:public Base3,public Base1
{
public:
    Base4(){ cout<<"Base4"<<endl; }
};


class Test:public Base1,public virtual Base2,public virtual Base3,public Base4
{
public:
    Test():Base4(),Base3(),Base2(),Base1(),obj2(),obj1()
    { cout<<"Test"<<endl; }
private:
    OBJ2 obj2;
    OBJ1 obj1;
};

int main()
{
    Test t;
}

bubuko.com,布布扣

多继承的构造顺序

标签:style   blog   http   color   io   os   sp   div   2014   

原文地址:http://www.cnblogs.com/huangcongcong/p/4003632.html

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