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

        <bdo id='RDXnG'></bdo><ul id='RDXnG'></ul>
      <tfoot id='RDXnG'></tfoot>
    1. <legend id='RDXnG'><style id='RDXnG'><dir id='RDXnG'><q id='RDXnG'></q></dir></style></legend>

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

      1. 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗?

        时间:2023-09-27

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

              <tbody id='b0toC'></tbody>

              • <bdo id='b0toC'></bdo><ul id='b0toC'></ul>
                  <legend id='b0toC'><style id='b0toC'><dir id='b0toC'><q id='b0toC'></q></dir></style></legend><tfoot id='b0toC'></tfoot>

                • 本文介绍了我应该对 C++ 中的成员变量和函数参数使用相同的名称吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想知道在 C++ 中为 成员变量 和函数 参数 使用相同的名称是否是一个好习惯.

                  I am wondering if it is a good practice to use the same name for both a member variable and a function parameter in C++.

                  我有 Java 背景,这很常见.我想知道在 C++ 中执行以下操作是否存在缺点(代码有效):

                  I come from a Java background, where this was common. I am wondering if in C++ there are drawbacks doing the following (the code works):

                  class Player
                  {
                      public:
                      void setState(PlayerState *state)
                      {
                          this->state = state;
                      }
                  
                      private:
                         PlayerState *state;
                  }
                  

                  <小时>

                  感谢您的回答.据我了解,虽然它有效,但更好的做法是放置某种标记来区分成员变量和函数参数,例如:


                  Thank you for the answers. As I understand while it works, a better practice would be to put some kind of marker to differentiate member variable from function parameters like:

                   _ or m_
                  

                  在某些编辑器(如 Qt Designer)中,成员变量以不同的颜色显示.这就是为什么似乎没有必要添加任何前缀.

                  In some editors (like Qt Designer), member variables are shows in a different color. This is why it did not seem necessary to add any prefixes.

                  推荐答案

                  这是正确的,并且是标准所允许的.但更好的方法是对成员变量使用一些命名约定.例如,您可以为所有成员变量使用 m_ 前缀,然后任何人都可以推断出 m_state 是什么.增加了代码的可读性,避免了常见错误.

                  That is correct, and allowed by the Standard. But a better approach is to use some naming-convention for member variables. For example, you could use m_ prefix for all member variables, then anyone could infer what m_state is. It increases the readability of the code, and avoids common mistakes.

                  另外,如果m_state是成员,那么就不必在成员函数中写this->m_state = state,直接写<代码>m_state = 状态.在您当前的代码中,this-> 部分变得必要,否则 state = state 将成为自赋值.

                  Also, if m_state is the member, then you don't have to write this->m_state = state in the member function, you could just write m_state = state. In your current code, this-> part becomes necessary, without which state = state will become self-assignment.

                  这篇关于我应该对 C++ 中的成员变量和函数参数使用相同的名称吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:可选函数参数:使用默认参数 (NULL) 还是重载函数? 下一篇:if 语句中变量的范围

                  相关文章

                  <legend id='ZN6Yr'><style id='ZN6Yr'><dir id='ZN6Yr'><q id='ZN6Yr'></q></dir></style></legend>

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

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

                    <tfoot id='ZN6Yr'></tfoot>
                      <bdo id='ZN6Yr'></bdo><ul id='ZN6Yr'></ul>