<tfoot id='zmMOG'></tfoot>

    1. <i id='zmMOG'><tr id='zmMOG'><dt id='zmMOG'><q id='zmMOG'><span id='zmMOG'><b id='zmMOG'><form id='zmMOG'><ins id='zmMOG'></ins><ul id='zmMOG'></ul><sub id='zmMOG'></sub></form><legend id='zmMOG'></legend><bdo id='zmMOG'><pre id='zmMOG'><center id='zmMOG'></center></pre></bdo></b><th id='zmMOG'></th></span></q></dt></tr></i><div id='zmMOG'><tfoot id='zmMOG'></tfoot><dl id='zmMOG'><fieldset id='zmMOG'></fieldset></dl></div>
        <bdo id='zmMOG'></bdo><ul id='zmMOG'></ul>
      <legend id='zmMOG'><style id='zmMOG'><dir id='zmMOG'><q id='zmMOG'></q></dir></style></legend>

      <small id='zmMOG'></small><noframes id='zmMOG'>

      1. 我可以对字段和构造函数参数使用相同的名称吗?

        Can I use identical names for fields and constructor parameters?(我可以对字段和构造函数参数使用相同的名称吗?)

        <i id='zxgfg'><tr id='zxgfg'><dt id='zxgfg'><q id='zxgfg'><span id='zxgfg'><b id='zxgfg'><form id='zxgfg'><ins id='zxgfg'></ins><ul id='zxgfg'></ul><sub id='zxgfg'></sub></form><legend id='zxgfg'></legend><bdo id='zxgfg'><pre id='zxgfg'><center id='zxgfg'></center></pre></bdo></b><th id='zxgfg'></th></span></q></dt></tr></i><div id='zxgfg'><tfoot id='zxgfg'></tfoot><dl id='zxgfg'><fieldset id='zxgfg'></fieldset></dl></div>

              <legend id='zxgfg'><style id='zxgfg'><dir id='zxgfg'><q id='zxgfg'></q></dir></style></legend>
            1. <small id='zxgfg'></small><noframes id='zxgfg'>

                <tfoot id='zxgfg'></tfoot>
                  <bdo id='zxgfg'></bdo><ul id='zxgfg'></ul>
                    <tbody id='zxgfg'></tbody>
                  本文介绍了我可以对字段和构造函数参数使用相同的名称吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  
                  class C {
                    T a;
                  public:
                    C(T a): a(a) {;}
                  };
                  

                  合法吗?

                  推荐答案

                  是的,它是合法的,适用于所有平台.它将正确地将您的成员变量 a 初始化为传入的值 a.

                  Yes it is legal and works on all platforms. It will correctly initialize your member variable a, to the passed in value a.

                  一些更干净的人认为以不同的方式命名它们,但不是全部.我个人实际上经常使用它:)

                  It is considered by some more clean to name them differently though, but not all. I personally actually use it a lot :)

                  具有相同变量名的初始化列表之所以有效,是因为初始化列表中的初始化项的语法如下:

                  Initialization lists with the same variable name works because the syntax of an initialization item in an initialization list is as follows:

                  <成员>(<值>)

                  你可以通过创建一个简单的程序来验证我上面写的内容:(它不会编译)

                  You can verify what I wrote above by creating a simple program that does this: (It will not compile)

                  class  A
                  {
                  
                     A(int a)
                     : a(5)//<--- try to initialize a non member variable to 5
                     {
                     }
                  };
                  

                  您将收到类似如下的编译错误:A 没有名为 'a' 的字段.

                  You will get a compiling error something like: A does not have a field named 'a'.

                  附注:

                  可能不希望使用与参数名称相同的成员名称的一个原因是您更容易出现以下情况:

                  One reason why you may not want to use the same member name as parameter name is that you would be more prone to the following:

                  class  A
                  {
                  
                     A(int myVarriable)
                     : myVariable(myVariable)//<--- Bug, there was a typo in the parameter name, myVariable will never be initialized properly
                     {
                     }
                     int myVariable;
                  };
                  

                  <小时>

                  附注(2):


                  On a side note(2):

                  可能希望使用与参数名称相同的成员名称的一个原因是您不太可能出现以下情况:

                  One reason why you may want to use the same member name as parameter name is that you would be less prone to the following:

                  class  A
                  {
                  
                     A(int myVariable_)
                     {
                       //<-- do something with _myVariable, oops _myVariable wasn't initialized yet
                       ...
                       _myVariable = myVariable_;
                     }
                     int _myVariable;
                  };
                  

                  这也可能发生在大型初始化列表中,并且您在初始化列表中初始化它之前使用了 _myVariable.

                  This could also happen with large initialization lists and you use _myVariable before initializing it in the initialization list.

                  这篇关于我可以对字段和构造函数参数使用相同的名称吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)

                    <legend id='OCyT3'><style id='OCyT3'><dir id='OCyT3'><q id='OCyT3'></q></dir></style></legend>
                          <tbody id='OCyT3'></tbody>
                      • <tfoot id='OCyT3'></tfoot>
                          <bdo id='OCyT3'></bdo><ul id='OCyT3'></ul>
                            <i id='OCyT3'><tr id='OCyT3'><dt id='OCyT3'><q id='OCyT3'><span id='OCyT3'><b id='OCyT3'><form id='OCyT3'><ins id='OCyT3'></ins><ul id='OCyT3'></ul><sub id='OCyT3'></sub></form><legend id='OCyT3'></legend><bdo id='OCyT3'><pre id='OCyT3'><center id='OCyT3'></center></pre></bdo></b><th id='OCyT3'></th></span></q></dt></tr></i><div id='OCyT3'><tfoot id='OCyT3'></tfoot><dl id='OCyT3'><fieldset id='OCyT3'></fieldset></dl></div>
                          1. <small id='OCyT3'></small><noframes id='OCyT3'>