标题:为什么结果是这样的,请高人指点啊!
取消只看楼主
chao19910210
Rank: 1
等 级:新手上路
帖 子:12
专家分:4
注 册:2011-11-18
结帖率:80%
已结贴  问题点数:16 回复次数: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(c),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;
}
结果:

搜索更多相关主题的帖子: private include public return 
2012-07-06 13:56



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




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

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