【发布时间】:2017-12-01 14:21:34
【问题描述】:
我在 Destructor 上做了实践,但是当编译这个程序时,我不知道为什么输出没有像我想的那样。
#include <iostream>
using namespace std;
class aaa
{
private:
static int x;
int code;
public:
/*after constructor executes 3 times the value of "code" becomes 103*/
aaa()
{
code=x;
cout<<"Default Constructor"<<endl;
x++;
}
~aaa()
{
cout<<"Destructor of "<<code<<endl;
}
};
int aaa::x=101;
int main()
{
aaa *p;
p=new aaa[3];
delete []p;
return 0;
}
输出是:
Default Constructor
Default Constructor
Default Constructor
Destructor of 103
Destructor of 102
Destructor of 101
虽然我以为会是这样:
101
102
103
【问题讨论】:
标签: c++ arrays oop destructor new-operator