什么是 C++ 中的就地构造函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3763846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is an in-place constructor in C++?
提问by Akhil
Possible Duplicate:
C++'s “placement new”
可能的重复:
C++ 的“placement new”
What is an in-place constructor in C++?
什么是 C++ 中的就地构造函数?
e.g. Datatype *x = new(y) Datatype();
例如数据类型*x = new(y) 数据类型();
回答by linuxuser27
This is called the placement new operator. It allows you to supply the memory the data will be allocated in without having the newoperator allocate it. For example:
这称为放置新运算符。它允许您提供将分配数据的内存,而无需new操作员分配它。例如:
Foo * f = new Foo();
The above will allocate memory for you.
以上将为您分配内存。
void * fm = malloc(sizeof(Foo));
Foo *f = new (fm) Foo();
The above will use the memory allocated by the call to malloc. newwill not allocate any more. You are not, however, limited to classes. You can use a placement new operator for any type you would allocate with a call to new.
以上将使用调用分配的内存malloc。 new不会再分配了。但是,您不仅限于课程。您可以对通过调用分配的任何类型使用放置 new 运算符new。
A 'gotcha' for placement new is that you should notrelease the memory allocated by a call to the placement new operator using the deletekeyword. You will destroy the object by calling the destructor directly.
放置 new 的一个“问题”是您不应该释放通过使用delete关键字调用放置 new 运算符分配的内存。您将通过直接调用析构函数来销毁对象。
f->~Foo();
After the destructor is manually called, the memory can then be freed as expected.
手动调用析构函数后,内存可以按预期释放。
free(fm);
回答by Michael Kristofik
The short answer is that your code constructs an object in the space pointed to by y. The long answer is best covered by the C++ FAQ.
简短的回答是您的代码在 指向的空间中构造了一个对象y。长答案最好在C++ FAQ 中介绍。
回答by Michael Burr
This is more commonly known as 'placement new' and is discussed pretty well by the C++ FAQ (in the 'Destructors' area):
这通常被称为“新放置”,C++ 常见问题解答(在“析构函数”区域)中对此进行了很好的讨论:
It allows you to construct objects in raw memory, which can be useful in certain specialized situations, such as when you might want to allocate an array for a large number of possible objects, but want to construct then as needed because you often might not need anywhere near the maximum, or because you want or need to use a custom memory allocator.
它允许您在原始内存中构造对象,这在某些特殊情况下很有用,例如当您可能想要为大量可能的对象分配一个数组,但想要根据需要构造然后因为您通常可能不需要接近最大值的任何地方,或者因为您想要或需要使用自定义内存分配器。
回答by wheaties
I'm rusty on this one but it allows you to write the object to a memory block you have already allocated. It also needs a reciprocal delete statement to clear it from memory.
我对这个很生疏,但它允许您将对象写入已分配的内存块。它还需要一个相互删除语句来从内存中清除它。
回答by stonemetal
If you use a memory pool, then you need to use the in place constructor to initialize your object as they are allocated from the pool.
如果您使用内存池,那么您需要使用就地构造函数来初始化您的对象,因为它们是从池中分配的。
回答by Fozi
It's a way to call a constructor without allocating memory. Your yhas to be a pointer poniting to enough memory for a new Datatype object. Also, don't call delete, call ~DataType().
这是一种无需分配内存即可调用构造函数的方法。您y必须是一个指向新数据类型对象的足够内存的指针。另外,不要打电话delete,打电话~DataType()。

