Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean for (placement_params) the parentheses after new operator and before type in c++?

What does the first parenthesis do?

// TArray<struct FBatchedLine> BatchedLines;      // declared in LineBatchComponent.h

new(BatchedLines) FBatchedLine(Start, End, Color, LifeTime, Thickness, DepthPriority);

The new operator reference says it is a placement_params:

If placement_params are provided, they are passed to the allocation function as additional arguments

I suppose it creates the object directly at the end of the given array BatchedLines, but I am not sure to understand fully how it works.

When are placement_params useful?

Note

For those who have an access to the github UnrealEngine repo, here is the source file.

like image 515
arthur.sw Avatar asked Jan 14 '16 16:01

arthur.sw


1 Answers

This is placement new syntax. It constructs an object of type FBatchedLine at the memory pointed to by BatchedLines with the constructor arguments (Start, End, Color, LifeTime, Thickness, DepthPriority). After the call, BatchedLines can be used to reference the constructed object.

Informally, you could imagine invoking the constructor with BatchedLines being this.

like image 148
Lingxi Avatar answered Nov 15 '22 09:11

Lingxi