c++ - What does new (this) mean? -


i found code sample study:

t & t::operator=(t const & x) {   if (this != &x)   {     this->~t(); // destroy in place     new (this) t(x); // construct in place   }   return *this; } 

when @ the documentation new there no version takes pointer. thus:

  1. what new (this) mean?
  2. what used for?
  3. how can called if not listed in documentation?

it called "placement new", , comments in code snippet pretty explain it:

it constructs object of type t without allocating memory it, in address specified in parentheses.

so you're looking @ copy assignment operator first destroys object being copied (without freeing memory), , constructs new 1 in same memory address. (it pretty bad idea implement operator in manner, pointed out in comments)


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -