标题:这个程序构造函数的顺序是怎么样的
只看楼主
不同认为
Rank: 1
等 级:新手上路
帖 子:93
专家分:3
注 册:2015-11-25
结帖率:57.14%
 问题点数:0 回复次数:0 
这个程序构造函数的顺序是怎么样的
#include<iostream>
using namespace std;
class A{
public:
    A(int a):x(a){ cout<<"A constructor..."<<x<<endl;    }
    int f(){return ++x;}
    ~A(){cout<<"destructor A..."<<endl;}
private:
    int x;
};
class B:public virtual A{
private:
    int y;
    A Aobj;
public:
    B(int a,int b,int c):A(a),y(c),Aobj(c){ cout<<"B constructor..."<<y<<endl;}
    int f(){
        A::f();
        Aobj.f();
        return ++y;
    }
    void display(){    cout<<A::f()<<"\t"<<Aobj.f()<<"\t"<<f()<<endl;    }
    ~B(){cout<<"destructor B..."<<endl;}   
};
class C:public B{
public:
    C(int a,int b,int c):B(a,b,c),A(0){ cout<<"C constructor..."<<endl;}
};
class D:public C,public virtual A{
public:
    D(int a,int b,int c):C(a,b,c),A(c){ cout<<"D constructor..."<<endl;}
    ~D(){cout<<"destructor D...."<<endl;}
};
int main()
{
    D d(7,8,9);
    d.f();
    d.display();
return 0;
}
怎么执行的
2016-05-08 20:42



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-464538-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.102417 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved