C++,是否可以直接调用构造函数,而不需要new?
如果我已经有对象的内存,我可以显式调用构造函数而不使用 new 吗?
class Object1{
char *str;
public:
Object1(char*str1){
str=strdup(str1);
puts("ctor");
puts(str);
}
~Object1(){
puts("dtor");
puts(str);
free(str);
}
};
Object1 ooo[2] = {
Object1("I'm the first object"), Object1("I'm the 2nd")
};
do_smth_useful(ooo);
ooo[0].~Object1(); // call destructor
ooo[0].Object1("I'm the 3rd object in place of first"); // ???? - reuse memory
Can I call constructor explicitly, without using new
, if I already have a memory for object?
class Object1{
char *str;
public:
Object1(char*str1){
str=strdup(str1);
puts("ctor");
puts(str);
}
~Object1(){
puts("dtor");
puts(str);
free(str);
}
};
Object1 ooo[2] = {
Object1("I'm the first object"), Object1("I'm the 2nd")
};
do_smth_useful(ooo);
ooo[0].~Object1(); // call destructor
ooo[0].Object1("I'm the 3rd object in place of first"); // ???? - reuse memory
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
有点像。您可以使用 placement new 使用已分配的内存运行构造函数:
所以,您仍然使用
new
关键字,但不进行内存分配。Sort of. You can use placement new to run the constructor using already-allocated memory:
So, you're still using the
new
keyword, but no memory allocation takes place.让我向您展示一些关于如何在构造和销毁过程中完成此操作的代码,
我希望上面的摘要使事情变得更清楚。
Let me show you some code on how it can be done, both in construction and destruction
I hope the above summary makes things clearer.
我认为您正在寻找新的安置。 C++ FAQ Lite 很好地总结了如何做这个。本条目中有一些重要的问题:
#include
来使用placement new 语法。I think you're looking for Placement New. The C++ FAQ Lite has a good summary of how you do this. There are a few important gotchas from this entry:
#include <new>
to use the placement new syntax.从字面上讲,不,没有“new”关键字就无法做到这一点。请参阅有关放置 new 的所有答案,了解使用“new”关键字调用构造函数而不实际分配内存的方法。
Literally speaking, NO, you can't do it without the "new" keyword. See all the answers about placement new for the way to use the "new" keyword to call the constructor without actually allocating memory.
是的,当您拥有自己分配的缓冲区时,您可以使用新的放置。 Brian Bondy 在相关问题中给出了很好的答复:
“placement new”有什么用途?
Yes, when you've got your own allocated buffer you use placement new. Brian Bondy has a good response here in a related question:
What uses are there for "placement new"?
您可以调用析构函数,但内存不会被回收,并且您的调用将相当于函数调用。您必须记住,析构函数在下面做了两件事:根据您的规范析构对象,并回收内存。由于无论如何都会为堆栈上分配的对象调用 dtor,因此调用它两次可能会导致未定义的行为。
You can call a destructor, but memory will not be reclaimed, and your call will be equivalent to a function call. You have to remember that underneath the destructor does 2 things: destructs object based on your specification, and reclaims the memory. Since you dtor will be called anyway for an object allocated on the stack, calling it twice may result in an undefined behavior.
是的,使用placement new - 如上所述,但您可能会考虑使用第二个工厂类来管理存储,即使这意味着复制对象。 memcpy() 对于小对象来说通常很便宜。
Yes, using placement new - as above, but you might consider having a second factory class to manage the storage, even if it means copying an object. memcpy() is generally cheap for small objects.
您可以使用以下模板
用法:
You can use the following template
usage:
问题是,通常您需要一个已经存在的对象来调用函数(除非它是静态函数),因此对于构造函数来说,当您想以非标准方式执行此操作时,这有点问题。
以下是如何在现有内存中创建新的类对象而不使用
new
。这甚至适用于来自malloc()
的原始内存,前提是您首先memset()
将内存清零。可能的输出:
The problem is that normally you need an already existing object on which to call the functions (unless it is a static function), so for the constructor that's a bit of a an issue when you want to do it in a nonstandard way.
Here's how to create a new class object in existing memory without using
new
. This will even work with raw memory frommalloc()
, provided you firstmemset()
the memory to zeros.Possible output:
根据评论,这仅适用于 Microsoft C++ 编译器
非常简单,没有
new
:这适用于任何类:
Based on comments, this only works for Microsoft C++ compilers
Quite simply, without
new
:This works with any class: