主题:请解释这是为什么?
下面这程序其输出 的结果让我弄不懂,请大家指教。在VC6.0下调试。
#include<iostream>
using namespace std;
class B1
{
public:
B1(int i=0)
{
cout<<"Constructing B1"<<i<<endl;
}
~B1()
{
cout<<"Destructing B1"<<endl;
}
};
class B2
{
public:
B2(int j=1)
{
cout<<"Constructing B2"<<j<<endl;
}
~B2()
{
cout<<"Destructing B2"<<endl;
}
};
class C:public B2,public B1
{
public:
C(int a,int b):memberB2(a),B2(b)
{
cout<<"Constructing 我自己"<<endl;
}
~C()
{
cout<<"Destructing c"<<endl;
}
private:
B1 memberB1;
B2 memberB2;
};
void main()
{
C obj(10,20);
}
程序的结果为:
Constructing B220
Constructing B10
Constructing B10
Constructing B210
Constructing 我自己
Destructing c
Destructing B2
Destructing B1
Destructing B1
Destructing B2
Prress any key to continue.
请高手对输出结果一一解答,不胜感激。(另用其它更高版本的VC调试器好像结果还与这个不同。)
#include<iostream>
using namespace std;
class B1
{
public:
B1(int i=0)
{
cout<<"Constructing B1"<<i<<endl;
}
~B1()
{
cout<<"Destructing B1"<<endl;
}
};
class B2
{
public:
B2(int j=1)
{
cout<<"Constructing B2"<<j<<endl;
}
~B2()
{
cout<<"Destructing B2"<<endl;
}
};
class C:public B2,public B1
{
public:
C(int a,int b):memberB2(a),B2(b)
{
cout<<"Constructing 我自己"<<endl;
}
~C()
{
cout<<"Destructing c"<<endl;
}
private:
B1 memberB1;
B2 memberB2;
};
void main()
{
C obj(10,20);
}
程序的结果为:
Constructing B220
Constructing B10
Constructing B10
Constructing B210
Constructing 我自己
Destructing c
Destructing B2
Destructing B1
Destructing B1
Destructing B2
Prress any key to continue.
请高手对输出结果一一解答,不胜感激。(另用其它更高版本的VC调试器好像结果还与这个不同。)