0
点赞
收藏
分享

微信扫一扫

显式调用析构函数与栈中 new 对象



#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 在原地址重新创建类实例




举报

相关推荐

0 条评论