对于(placement_params)新运算符后和c ++中的类型之前的括号是什么意思?

问题描述 投票:2回答:2

第一个括号是做什么的?

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

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

new operator参考说它是placement_params

如果提供了placement_params,则将它们作为附加参数传递给分配函数

我想它直接在给定数组BatchedLines的末尾创建对象,但我不确定它是如何工作的完全理解的。

placement_params什么时候有用?

注意

对于那些有权访问github UnrealEngine repo的人来说,here是源文件。

c++ new-operator allocation
2个回答
4
投票

这是放置新语法。它在FBatchedLine指向的内存中使用构造函数参数BatchedLines构造一个(Start, End, Color, LifeTime, Thickness, DepthPriority)类型的对象。调用之后,BatchedLines可用于引用构造的对象。

非正式地,您可以想象使用BatchedLines作为this来调用构造函数。


0
投票

文字:palcement_params没有在reference C++ page中解释。根据英文含义:指针通常放在数组中的位置。这是boost的代码示例

T* buffer;
size_t write_index;
new (buffer + write_index) T(t); // copy-construct

实质上,您将类型为T的新对象放入缓冲区的write_index位置。

© www.soinside.com 2019 - 2024. All rights reserved.