Call destructor and then constructor (resetting an object)

后端 未结 10 1432
悲&欢浪女
悲&欢浪女 2020-12-05 18:09

I want to reset an object. Can I do it in the following way?

anObject->~AnObject();
anObject = new(anObject) AnObject();
// edit: this is not allowed: anO         


        
10条回答
  •  萌比男神i
    2020-12-05 18:42

    Why not reset using the operator=()? This is not debatable and by far more readable.

    A a;
    //do something that changes the state of a
    a = A(); // reset the thing
    

提交回复
热议问题