//显示调用析构函数的效果是适当的清除对象本身。但是,并没有释放对象所占的内存,如果需要,可以重用该内存空间。
-
#include<iostream>
-
#include<cstdio>
-
using namespace std;
-
-
-
class Test{
-
int i;
-
public:
-
Test(int i=0)
-
{
-
printf("constructor%d\n",i);
-
}
-
~Test()
-
{
-
printf("destroy\n");
-
}
-
};
-
-
-
int main()
-
{
-
/* Test *p=new Test;
-
p->~Test();
-
-
new (p)Test(1);//placement new
- p->~Test();
-
operator delete((void*)p);
- //delete p;
-
*/
-
Test t;
-
t.~Test();
-
new (&t)Test(1);//placement new
-
-
-
return 0;
- }