我应该使用初始化列表还是在我的 C++ 中执行分配?构造函数?
class Node
{
public:
Node *parent; // used during the search to record the parent of successor nodes
Node *child; // used after the search for the application to view the search in reverse
float g; // cost of this node + it's predecessors
float h; // heuristic estimate of distance to goal
float f; // sum of cumulative cost of predecessors and self and heuristic
Node() :
parent( 0 ),
child( 0 ),
g( 0.0f ),
h( 0.0f ),
f( 0.0f )
{
}
UserState m_UserState;
};
为什么我们应该使用构造函数
Node() :
parent( 0 ),
child( 0 ),
g( 0.0f ),
h( 0.0f ),
f( 0.0f )
{
}
而不是
Node()
{
parent = null;
child = null;
g = 0.0f;
h = 0.0f;
f = 0.0f;
}
谢谢:)
class Node
{
public:
Node *parent; // used during the search to record the parent of successor nodes
Node *child; // used after the search for the application to view the search in reverse
float g; // cost of this node + it's predecessors
float h; // heuristic estimate of distance to goal
float f; // sum of cumulative cost of predecessors and self and heuristic
Node() :
parent( 0 ),
child( 0 ),
g( 0.0f ),
h( 0.0f ),
f( 0.0f )
{
}
UserState m_UserState;
};
Why we should use the constructor
Node() :
parent( 0 ),
child( 0 ),
g( 0.0f ),
h( 0.0f ),
f( 0.0f )
{
}
instead of
Node()
{
parent = null;
child = null;
g = 0.0f;
h = 0.0f;
f = 0.0f;
}
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于普通的旧数据(POD),这没有什么好处,但是一旦您开始使用引用或组合类,它就会产生影响:
与
有时,一旦对象具有了,您甚至没有办法“重新构造”对象已被构造,因为类可能不支持 setter 或
operator=
。const
成员当然不能被“重新构造”或重置:编辑:感谢@Vitus 指出了引用的问题。
For plain old data (POD), this has little benefit, but once you start either using references or composing classes it makes a difference:
versus
Sometimes, you won't even have a way of "re-constructing" an object once it has been constructed, as a class may not support setters or
operator=
.const
members can certainly not be "re-constructed" or reset:Edit: thanks to @Vitus for pointing out the problem with references.
由于在某些情况下您实际上需要或由于性能原因应该使用初始化列表来初始化成员,因此您应该一致并始终使用它。
您需要使用初始化列表的示例是聚合没有默认构造函数或具有 const 成员(是的,不常见,但允许)。
例如,当您应该(但不强制)使用初始化列表时,您有一个聚合对象,并且以下几点有效:
那么现在 - 为什么你不应该使用它呢?
As there are some circumstances where you actually need or by performance reasons should initialize members using an initialization list, you should be consistent and always use it.
Examples where you need to use initialization lists is when an aggregate has no default constructor or when you have const members (yes, uncommon, but allowed).
An example where when you should (but aren't forced to) use initialization lists is when you have an aggregate object and the following points are valid:
Now then - why should you not use it?
首选初始化列表
Prefer initialization lists
主要是因为性能问题。
此处对此进行了讨论。
Mainly because of performance issues.
It's discussed here.
主要原因是对象有效性和类不变量。另一个原因是易于使用。
如果您将所有内容都放入构造函数中,那么当构造函数完成时,您将保证得到一个有效的 Node.js 文件。
如果必须单独设置所有实例变量,则可能会出现一个 Node 中的某些变量未初始化的情况,原因可能是程序员忘记了,或者某个赋值引发了异常。后者不会发生在你的情况下,但一般来说它可能会发生。如果节点未完全初始化,则无法保证其行为符合您的预期。
The main reason is object validity and class invariants. Another reason is ease of use.
If you put everything in the constructor then when the constructor finishes you are guaranteed a valid Node.
If you had to set all the instance variables separately then it would be possible to have a Node with some of the variables uninitialised either because the programmer forgot or if one of the assignments throws an exception. The latter would not occur in your case but in general it could. If the Node is not fully initialised its behaviour cannot be guaranteed to be as you expect.