This is enclosed in a for loop:
v[i] = new (&vv[i]) vertex(pts[i],i);
vertex
is astruct
pts
is apoint*
v
is avertex**
vv
is avertex*
What does the (&vv[i])
part do?
This is enclosed in a for loop:
v[i] = new (&vv[i]) vertex(pts[i],i);
vertex
is a struct
pts
is a point*
v
is a vertex**
vv
is a vertex*
What does the (&vv[i])
part do?
It looks like placement new. It's the same as an ordinary new
statement, but instead of actually allocating memory, it uses memory already available and which is pointed to by the expression inside the parentheses.
In your case it uses the memory in vv[i]
to create the new vertex
object, then returns a pointer to that (i.e. &vv[i]
) and it's assigned to v[i]
.
See e.g. this reference for more details.
That's placement new-expression.
It's constructing a new object at already allocated memory - at the address of vv[i]
.
It's calling this allocation function:
operator new(std::size_t, void*);
// ^^^^
// &vv[i] is passed here
which simply returns the second argument. The constructor of vertex
that matches the number of arguments of their types is then called to construct the object in place.
Yes,it is a placement new, it sepcify a address where the new Object place in. if your (&vv[i]) equal 0xabcdef00, your new object will be in 0xabcdef00, more detial you could check c++ standard 5.3.4.
This is placement new that allows placing an object at a specified memory location. See http://www.parashift.com/c++-faq/placement-new.html and What uses are there for "placement new"?.