如何设置调用析构函数才能实现以下效果
//////////////如何设置调用析构函数才能实现以下效果:::Object 1 is created, we've got 1 object(s) now!
Object 2 is created, we've got 2 object(s) now!
Object 3 is created, we've got 3 object(s) now!
Object 3 is deleted, we've got 2 object(s) now!
Object 2 is deleted, we've got 1 object(s) now!////////////这里
Object 4 is created, we've got 2 object(s) now!
Object 4 is deleted, we've got 1 object(s) now!
Object 1 is deleted, we've got 0 object(s) now!//////////到这里
//////////原码:
[code]#include<iostream>
using namespace std;
class Object
{
public:
Object(int i) {
id = i;
count++;
cout<<"Object "<<id<<" is created, we've got "<<count<<" object(s) now!"<<endl;
}
~Object() {
count--;
cout<<"Object "<<id<<" is deleted, we've got "<<count<<" object(s) now!"<<endl;
}
private:
int id;
static int count;
};
int Object::count = 0;
int main()
{
Object object[3]{
Object(1),
Object(2),
Object(3)
};
return 0;
}
[此贴子已经被作者于2017-3-11 16:02编辑过]