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

来源:互联网 发布:知乎 伊朗小公主 编辑:程序博客网 时间:2024/06/06 07:34


#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 0
原创粉丝点击