What is the meaning of the below sentence in C ++
Possible duplicate:
"New C ++ Placement"
in the code below what is line 3, is this a way to cast? or what
void someCode()
{
char memory[sizeof(Fred)]; // Line #1
void* place = memory; // Line #2
Fred* f = new(place) Fred(); // Line #3
// The pointers f and place will be equal
...
}
+3
source to share
1 answer
This is a typical use placement new .
It allows you to allocate memory and then build objects at that specific memory location.
Line # 3 essentially just calls the constructor Fred::Fred()
. The pointer this
in the constructor Fred
will be place
. The returned pointer f
will therefore be equal place
.
+3
source to share