C++ 的 new 运算符不仅分配内存,还会在该内存上执行某个构造函数。对于底层软件,可能已经为新对象预先分配了内存,而只需要运行其构造函数。为此,可以使用“定位 new”(placement new),如下所示:

CRType* poVar = new (preallocation) CRType;

这样,CRType 的构造函数会在 preallocation 指定的内存地址上执行,而不会重新分配内存。

最后修改: 2025年01月30日 星期四 12:04