C++程序问题,不会啊,555,请求帮助

来源:百度知道 编辑:UC知道 时间:2024/06/28 22:51:53
1.

#include<iostream.h>
class B
{
public:
B()
{cout<<++b<<endl;}
~B()
{cout<<b--<<endl;}
static int Getb()
{return b;}
private:
static int b;
};
int B::b=10;
void main()
{
B b1,b2,b3;
cout<<B::Getb()<<endl;
}
输出结果是
11
12
13
13
13
12
11
不懂啊,请帮我分析一下过程啊,感谢了

B b1,b2,b3;
时调用 构造函数B()
{cout<<++b<<endl;} 3次
所以输出 11 12 13

然后调用
cout<<B::Getb()<<endl;

static int Getb()
{return b;}
返回13
最后结束时要调用3次析构函数
~B()
{cout<<b--<<endl;}
所以输出
13 12 11

上面说的很对啊