How to delete object constructed via placement new operator?

前端 未结 4 2127
谎友^
谎友^ 2020-12-09 09:38
char * buf = new char[sizeof(T)];
new (buf) T;
T * t = (T *)buf;
//code...
//here I should destruct *t but as it is argument of template and can be
//instantiated vi         


        
4条回答
  •  北海茫月
    2020-12-09 09:54

    You first destruct the object by directly calling the destructor:

    t->~T();
    

    Then you destroy the memory by calling delete[] on the pointer returned from new[]:

    delete []buf;
    

提交回复
热议问题