Calling a constructor to re-initialize object

前端 未结 12 1710
执笔经年
执笔经年 2020-12-04 15:01

is it possible to re-initialize an object of a class using its constructor?

12条回答
  •  有刺的猬
    2020-12-04 15:46

    Literally? Yes, by using placement new. But first you have to destruct the previously constructed object.

    SomeClass object(1, 2, 3);
    ...
    object.~SomeClass(); // destruct
    new(&object) SomeClass(4, 5, 6); // reconstruct
    ...
    // Final destruction will be done implicitly
    

    The value of this does not go beyond purely theoretical though. Don't do it in practice. The whole thing is ugly beyond description.

提交回复
热议问题