函数名和花括号之间的成员变量赋值的名称和原因是什么?
看一下这个代码片段:
Size::Size(int iSetWidth, int iSetHeight)
:iWidth(iSetWidth),
iHeight(iSetHeight)
{
}
据说,这意味着同样的事情:
Size::Size(int iSetWidth, int iSetHeight)
{
iWidth=iSetWidth;
iHeight=iSetHeight;
}
为什么你会使用前者或后者?前者叫什么名字?
Look at this code snippet:
Size::Size(int iSetWidth, int iSetHeight)
:iWidth(iSetWidth),
iHeight(iSetHeight)
{
}
Supposedly, this means the same thing as:
Size::Size(int iSetWidth, int iSetHeight)
{
iWidth=iSetWidth;
iHeight=iSetHeight;
}
Why would you use the former or the latter? And what is the name of the former?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,它们的意思并不完全相同。
当执行构造函数时,在进入代码块(大括号之间的代码)之前,它会构造所有对象数据成员。您在初始化程序(冒号后面和大括号之前的代码)中所做的就是指定对这些成员使用哪些构造函数。如果没有为特定数据成员指定构造函数,则将使用默认构造函数。
因此,如果您使用初始化列表(第一个示例),则将为每个成员使用正确的构造函数,并且不需要额外的代码。如果不这样做,首先使用默认构造函数,然后执行花括号内的代码。
总之:
编辑:抱歉,忘记在最后一行回答您的问题。
冒号和花括号之间的代码名称是初始化列表。
如果您知道变量或数据成员的正确构造函数,请务必使用它。这就是为什么大多数类都有不同的构造函数而不仅仅是默认构造函数的原因。所以你最好使用初始化列表。
初始化列表几乎不会比其他技术慢,而且很容易会更快。编写代码时的一条众所周知的规则是“不要过早优化”,但还有一个不太为人所知的规则:不要过早悲观。如果您有两种选择来编写一段代码,并且其中一个比另一个更好,但不涉及额外的工作或复杂性,请使用它。在您的示例中没有区别,因为您使用的是内置类型(
int
)。但如果您使用类,就会有区别,所以要习惯初始化列表。No, they don't mean exactly the same.
When a constructor is executed, before entering the code block (the code between the curly braces), it constructs all object data members. What you do in the initializers (the code after the colon and before the curly braces) is to specify which constructors to use for those members. If you don't specify a constructor for a specific data member, the default constructor will be used.
So, if you use the initialization list (first example), the right constructors will be used for each member and no additional code is necessary. If you don't, first the default constructor is used and then the code inside the curly braces is executed.
In summary:
EDIT: Sorry, forgot to answer your questions in the last line.
The name of the code between the colon and the curly braces is initialisation list.
If you know which is the right constructor for a variable or data member, by all means use it. This is the reason why most classes have different constructors instead of just a default constructor. So you are better off using the initialization list.
The initialisation list is almost never slower than the other technique, and can easily be faster. A well known rule when writing code is "don't optimize prematurely", but there is a not so well known counterpart: don't pessimize prematurely. If you have two options for writing a piece of code and one of them can be better than the other, but does not involve additional work or complexity, use it. In your example there is no difference, since you are using a built-in type (
int
). But if you were using classes, there would be a difference, so get used to the initialization list.前者称为初始化列表。
您可以获得大量相关文章。
这里给出了使用初始化器列表的具体原因
http://www.learncpp.com/cpp-tutorial/101 -constructor-initialization-lists/
您可以参考Effective C++ 来全面了解初始化器列表。
希望它是清楚的。
THe former is called initilazation lists.
You can get plentyof articles for that.
The particular reasons for using intializer lists are given here
http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/
You can refer Effective C++ to get full insight into intializer lists.
Hope it is clear.
顺便说一句,Bjarne Stroustrup 在C++ 编程语言中说,使用初始化列表可以提高一些效率,他建议我们使用初始化列表!
BTW, Bjarne Stroustrup said in The C++ Programming Language that some efficiency may be gained with initialization list and he recommended us to use initialization list!