没有 new 的 C++ 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1764831/
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
C++ Object without new
提问by Henry B
this is a really simple question but I havn't done c++ properly for years and so I'm a little baffled by this. Also, it's not the easiest thing (for me at least) to look up on the internet, not for trying.
这是一个非常简单的问题,但我多年来一直没有正确地使用 C++,所以我对此感到有些困惑。此外,在互联网上查找不是最简单的事情(至少对我而言),而不是尝试。
Why doesn't this use the new
keyword and how does it work?
为什么不使用new
关键字以及它是如何工作的?
Basically, what's going on here?
基本上,这里发生了什么?
CPlayer newPlayer = CPlayer(position, attacker);
采纳答案by AraK
This expression:
这个表达:
CPlayer(position, attacker)
creates a temporary object of type CPlayer
using the above constructor, then:
CPlayer
使用上述构造函数创建一个类型的临时对象,然后:
CPlayer newPlayer =...;
The mentioned temporary object gets copied using the copy constructor to newPlayer
. A better way is to write the following to avoid temporaries:
提到的临时对象使用复制构造函数复制到newPlayer
. 更好的方法是编写以下内容以避免临时:
CPlayer newPlayer(position, attacker);
回答by Timo Geusch
The above constructs a CPlayer object on the stack, hence it doesn't need new
. You only need to use new
if you are trying to allocate a CPlayer object on the heap. If you're using heap allocation, the code would look like this:
上面在堆栈上构造了一个 CPlayer 对象,因此它不需要new
. 仅new
当您尝试在堆上分配 CPlayer 对象时才需要使用。如果您使用堆分配,则代码如下所示:
CPlayer *newPlayer = new CPlayer(position, attacker);
Notice that in this case we're using a pointer to a CPlayer object that will need to be cleaned up by a matching call to delete
. An object allocated on the stack will be destroyed automatically when it goes out of scope.
请注意,在这种情况下,我们使用了一个指向 CPlayer 对象的指针,该对象需要通过对delete
. 分配在堆栈上的对象在超出范围时将自动销毁。
Actually it would have been easier and more obvious to write:
实际上,写起来会更容易也更明显:
CPlayer newPlayer(position, attacker);
A lot of compilers will optimise the version you posted to the above anyway and it's clearer to read.
无论如何,许多编译器都会优化您发布到上面的版本,并且阅读起来更清晰。
回答by Michael Kristofik
CPlayer newPlayer = CPlayer(position, attacker);
This line creates a new local object of type CPlayer. Despite its function-like appearance, this simply calls CPlayer's constructor. No temporaries or copying are involved. The object named newPlayer lives as long as the scope it's enclosed in. You don't use the new
keyword here because C++ isn't Java.
这一行创建了一个新的 CPlayer 类型的本地对象。尽管它看起来像函数,但这只是调用了 CPlayer 的构造函数。不涉及临时或复制。名为 newPlayer 的对象在它所包含的范围内一直存在。new
这里不要使用关键字,因为 C++ 不是 Java。
CPlayer* newPlayer = new CPlayer(position, attacker);
This line constructs a CPlayer object on the heap and defines a pointer named newPlayer to point at it. The object lives until someone delete
s it.
这一行在堆上构造了一个 CPlayer 对象,并定义了一个名为 newPlayer 的指针指向它。对象一直存在,直到有人找到delete
它为止。
回答by digitalarbeiter
newPlayer is no dynamically allocated variable but an auto, stack-allocated variable:
newPlayer 不是动态分配的变量,而是一个自动的、堆栈分配的变量:
CPlayer* newPlayer = new CPlayer(pos, attacker);
is different from
不同于
CPlayer newPlayer = CPlayer(pos, attacker);
newPlayer is allocated on the stack via the normal CPlayer(position, attacker) constructor invocation, though somewhat verbose than the usual
newPlayer 通过普通的 CPlayer(position,attacker) 构造函数调用在堆栈上分配,虽然比通常的有点冗长
CPlayer newPlayer(pos, attacker);
It's basically the same as saying:
它基本上与说相同:
int i = int(3);