Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

struct

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?

like image 781
ackerleytng Avatar asked Dec 15 '22 07:12

ackerleytng


1 Answers

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.

like image 50
Some programmer dude Avatar answered Jan 01 '23 18:01

Some programmer dude