c++ 中初始化程序和默认初始化程序列表之间的区别
你好, 我有一个问题但是找了好久都找不到答案,那就是 下面关于参数的两种说法有什么区别 初始化?
class A::A()
: a(0), b(0), c(0)
{
}
class A::A()
{
a = 0
b = 0;
c = 0;
}
我知道有“直接初始化”和“复制初始化”,但我 不知道还有什么区别以及是否有任何描述 关于第一个声明?
提前致谢
HI,
I have a question but cannot find the answer for a long time, that is,
what's difference between the following 2 statements about the parameter
initialization?
class A::A()
: a(0), b(0), c(0)
{
}
class A::A()
{
a = 0
b = 0;
c = 0;
}
I know there is "direct initialization" and "copy initialization", but I
don't know what's the other differences and if there is any description
about the first statement?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用初始值设定项列表,仅使用给定值创建和初始化成员一次。
使用赋值,成员会使用默认值进行初始化,然后在构造函数主体中重新赋值。
在某些情况下(常量、引用),您只能使用初始化列表,因为
在其他情况下,即使可以进行赋值,初始化列表仍然更可取,因为它避免了额外的工作(双重初始化,这对于某些类型来说可能成本高昂)并遵循常见的习惯用法,使您的代码更易于理解和维护。
需要注意的是,成员的初始化顺序是由其声明顺序定义的,而不是由它们在初始化列表中列出的顺序定义。例如,
会产生未定义的行为,因为
b
是在c
之前初始化的,因此具有未定义的值。为了避免混淆和潜在的此类微妙错误,请始终按照与在类中声明的顺序相同的顺序在初始化列表中列出成员。乍一看这似乎很晦涩,但这是有原因的。在 C++ 中,可以保证类的所有成员都按照与创建顺序完全相反的顺序被销毁。现在,类可以有多个构造函数,每个构造函数都有自己的初始化列表,并且(不幸的是,有人可能会说)初始化列表可以按程序员想要的任何方式排序。如果初始化列表的顺序决定了初始化的实际顺序,则运行时应该以某种方式维护有关每个对象的数据,以记住它是使用哪个构造函数创建的,以及应以什么顺序销毁其成员。这会产生运行时开销,但没有明显的好处,因此 - 符合“只为你使用的东西付费”的一般 C++ 哲学 - 决定初始化顺序由声明顺序一劳永逸地定义。
Using the initializer list, the members are created and initialized only once, with the given value.
Using assignment, the members are initialized with a default value, then reassigned in the constructor body.
In some cases (constants, references) you can only use initialization lists because
In other cases, even though assignment is possible, initialization list is still preferable as it avoids extra work (double initialization, which can be costly for some types) and adheres to a common idiom, making your code easier to understand and maintain.
One caveat which is good to know is that the order of initialization of members is defined by their order of declaration, not by the order in which they are listed in the initialization list. E.g.
yields undefined behaviour because
b
is initialized beforec
, hence with an undefined value. To avoid confusion and the potential for such subtle bugs, always list members in the initialization list in the same order as they're declared in the class.This may seem obscure at first, but there is a reason for it. In C++, it is guaranteed that all members of a class are destroyed in exactly the reverse of the order they were created. Now, classes can have multiple constructors, each with its own initialization list, and (unfortunately, one might say) initialization lists can be ordered any way the programmer wants. If the order of the initialization list determined the actual order of initialization, the runtime should somehow maintain data about each object to remember which constructor it was created with, and in what order its members should be destroyed. This would incure runtime overhead, for no obvious benefit, so - in line with the general C++ philosophy of "pay only for what you use" - it was decided that the initialization order is defined by the declaration order once and for all.
默认初始化列表的目的是初始化类中的常量变量。
因为常量变量是在对象初始化之前初始化的。
我提供一个示例来解释这两种初始化之间的区别:
The purpose of default initialization list is to initialize the constant variable with in the class.
Because the constant variable is initialized before the object is initialized.
I provide one sample to explain the difference between these two initializations:
主要区别在于,在第一种情况下,类成员被初始化,而在第二种情况下,它们被分配。对于非整数类型,这意味着在第二种情况下,您将使用
operator=
为类成员赋值。通常,最好使用第一种情况,因为在这种情况下,类成员在构造函数主体之前初始化。
另外,在某些情况下不能使用赋值,例如,当类成员声明为 const 时。
The main difference is, that in first case the clas members are initialized and in the second case they are assigned. For non integral types it means, that in the second case you will use
operator=
to assign values to your class members.Commonly, it's preferred to use first case, because in that case class memebers are initialized before the constructor body.
Also, you can't use assignment on several cases, for example, when class member is declared const.
摘自 Marshall Cline 的 C++ 常见问题解答第 10.6 节 :
Taken from section 10.6 of the C++ FAQ by Marshall Cline: