Is it possible to destroy and recreate an object in this way?

Possible duplicate:
Can I start a new launch in C ++ without restarting?

Consider T* o = new(T()) , where T has a copy constructor. Also suppose the new expression uses the default ::operator new()

To reuse the memory allocated for o instead of deleting the object with delete o , the standard resolves the following sequence:

  • calling o->~T() explicitly
  • use a new place to create a copy of the object in memory allocated for o earlier: new (o) T (x)
  • when executed with o and its memory, delete it with delete o

I also ask about this because I do not understand why std::map<T, V> (or its operator[] specifically), for example, requires that T have an appropriate assignment operator if the above sequence could work without it requirements. I doubt that the map was designed that way, because operator=() and the copy constructor can have different semantics, since most of the time they just implement the same way.

+7
source share

All Articles