#include<iostream>
using namespace std;
class A {
public:
A(string s){
this->s = s;
cout << "调用对象 "<<this->s<<" 默认构造函数" << endl;
}
~A() {
cout << "调用对象 " << this->s << " 析构函数" << endl << endl;
}
private:
string s;
};
int main(void) {
{ //当离开作用域时,析构函数会执行
A a("a");
}
{ //当离开作用域时,析构函数会执行
A b("b");
}
{ //当指向一个对象的引用或指针离开作用域前,应用delete析构函数会执行。
A *c = new A("c");
delete c;
}
{ //当指向一个对象的引用或指针离开作用域时,析构函数不会执行。
A *d = new A("d");
}
system("pause");
return 0;
}