I don't think this is casting, but what is it?

This is enclosed in a for loop:

v[i] = new (&vv[i]) vertex(pts[i],i); 
  • vertex is a struct
  • pts is point*
  • v is vertex**
  • vv is vertex*

What does the part (&vv[i]) do?

+4
source share
4 answers

Sounds like posting a new one. This is the same as the regular new operator, but instead of actually allocating memory, it uses the existing memory and pointed to by the expression inside the parentheses.

In your case, it uses the memory in vv[i] to create a new vertex object, then returns a pointer to it (ie &vv[i] ) and is assigned v[i] .

See this link for more details.

+9
source

This is the placement of a new expression .

It creates a new object in the already allocated memory - at vv[i] .

It calls this function:

 operator new(std::size_t, void*); // ^^^^ // &vv[i] is passed here 

which just returns the second argument. Then the vertex constructor, which corresponds to the number of arguments of their types, is called to construct the object in place.

+2
source

Yes, this is a new placement; it shares the address where the new property is located. If your (& vv [i]) is 0xabcdef00, your new object will be at 0xabcdef00, more detial you can check the C ++ 5.3.4 standard.

+1
source

This is a new placement that allows you to place an object in a specified memory location. See http://www.parashift.com/c++-faq/placement-new.html and What is used to “post new”? .

0
source

Source: https://habr.com/ru/post/1499780/


All Articles