函数名和花括号之间的成员变量分配的名称和原因是什么? [英] What is the name and reason for member variable assignment between function name and curly braces?

查看:217
本文介绍了函数名和花括号之间的成员变量分配的名称和原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看此代码片段:

  Size :: Size(int iSetWidth,int iSetHeight)
:iWidth (iSetWidth),
iHeight(iSetHeight)
{
}

假设这意味着同样的事情:

  Size :: Size(int iSetWidth,int iSetHeight)
{
iWidth = iSetWidth;
iHeight = iSetHeight;
}

为什么要使用前者或后者?

$ p

解决方案

不,它们的意思不完全一样。



当构造函数执行时,在输入代码块(大括号之间的代码)之前,它构造所有对象数据成员。在初始化器中(冒号之前的代码和花括号)之前的操作是指定要为那些成员使用哪些构造函数。如果你不为一个特定的数据成员指定一个构造函数,将使用默认的构造函数。



因此,如果你使用初始化列表右构造函数将用于每个成员,并且不需要附加代码。如果没有,首先使用默认构造函数,然后执行花括号中的代码。



总结:


$在你的第一个例子中,每个成员都使用适当的构造函数初始化,可能是复制构造函数。
  • 在第二个例子中,使用默认的构造函数构造,然后执行一些额外的代码来初始化它,可能是赋值运算符。



  • 冒号和大括号之间的代码名称是初始化列表。



    如果你知道哪个是一个变量或数据成员的正确构造函数,一定使用它。这就是为什么大多数类有不同的构造函数而不只是一个默认构造函数的原因。所以你最好使用初始化列表。



    初始化列表几乎从来没有比其他技术慢,并且可以很容易地更快。一个众所周知的规则当写代码是不优化过早,但有一个不那么熟悉的对手:不要过早地pessimize。如果你有两个选项来编写一段代码,其中一个可以比另一个更好,但不涉及额外的工作或复杂性,使用它。在你的例子中没有区别,因为你使用一个内置类型( int )。但是如果你使用类,会有一个区别,所以习惯了初始化列表。


    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?

    解决方案

    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:

    1. In your first example, each member is initialised using the appropriate constructor, probably the copy constructor.
    2. In your second example, each member is constructed using the default constructor, and then some additional code is executed to initialise it, probably the assignment operator.

    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.

    这篇关于函数名和花括号之间的成员变量分配的名称和原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆