0
class A
{
int id;
static int count;
public:
A()
{
count++;
id = count;
cout << "constructor called " << id << endl;
}
~A()
{
//count -=2; /*Keypoint is here. */
/*Uncomment it later. But result doesn't change*/
cout << "destructor called " << id << endl;
}
};
int A::count = 0;
int main()
{
A a[2];
return 0;
}
constructor called 1
constructor called 2
destructor called 2
destructor called 1
的問題是: 即使你取消註釋//count -=2;
結果還是一樣。
這是否意味着如果構造函數將靜態成員遞增1,那麼析構函數必須將其遞減1,並且您無法更改它的行爲?
Erm,析構函數打印'id',而不是'count'。你怎麼知道count會減少? –
哦,我很粗心,太傻了... – duleshi