#include <iostream>
#include <new>
class Test
{
public:
Test(int ii) : i(ii) {}
~Test() { std::cout << i << std::endl; }
public:
int i;
};
int main(int argc, char* argv[])
{
// #test will be destructed 2 times.
Test test(10);
Test* p = &test;
p->~Test();
new (p) Test(100);
return 0;
}
1. 显式调用析构函数
2. 通过 placement new 在原地址重新创建类实例