• <bdo id='bbf3S'></bdo><ul id='bbf3S'></ul>

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

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

        <tfoot id='bbf3S'></tfoot>

        在 C++ 中,阴影变量名称的范围解析(“优先顺序")是什么?

        时间:2023-10-17

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

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

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

              • <bdo id='XStv8'></bdo><ul id='XStv8'></ul>
                  <tfoot id='XStv8'></tfoot>

                    <tbody id='XStv8'></tbody>
                • 本文介绍了在 C++ 中,阴影变量名称的范围解析(“优先顺序")是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 C++ 中,shadowed 变量的范围解析(优先顺序")是什么名字?我似乎无法在网上找到简明的答案.

                  In C++, what is the scope resolution ("order of precedence") for shadowed variable names? I can't seem to find a concise answer online.

                  例如:

                  #include <iostream>
                  
                  int shadowed = 1;
                  
                  struct Foo
                  {
                      Foo() : shadowed(2) {}
                  
                      void bar(int shadowed = 3)
                      {
                          std::cout << shadowed << std::endl;
                              // What does this output?
                  
                          {
                              int shadowed = 4;
                              std::cout << shadowed << std::endl;
                                  // What does this output?
                          }
                      }
                  
                      int shadowed;
                  };
                  
                  
                  int main()
                  {
                      Foo().bar();
                  }
                  

                  我想不出任何其他变量可能会发生冲突的范围.如果我错过了,请告诉我.

                  I can't think of any other scopes where a variable might conflict. Please let me know if I missed one.

                  bar 成员函数中所有四个 shadow 变量的优先级顺序是什么?

                  What is the order of priority for all four shadow variables when inside the bar member function?

                  推荐答案

                  您的第一个示例输出 3.您的第二个输出 4.

                  Your first example outputs 3. Your second outputs 4.

                  一般的经验法则是查找从最局部"到最不局部"变量.因此,这里的优先级是块 -> 本地 -> 类 -> 全局.

                  The general rule of thumb is that lookup proceeds from the "most local" to the "least local" variable. Therefore, precedence here is block -> local -> class -> global.

                  您还可以显式访问每个阴影变量的大多数版本:

                  You can also access each most versions of the shadowed variable explicitly:

                  // See http://ideone.com/p8Ud5n
                  #include <iostream>
                  
                  int shadowed = 1;
                  
                  struct Foo
                  {
                      int shadowed;
                      Foo() : shadowed(2) {}
                      void bar(int shadowed = 3);
                  };
                  
                  void Foo::bar(int shadowed)
                  {
                      std::cout << ::shadowed << std::endl; //Prints 1
                      std::cout << this->shadowed << std::endl; //Prints 2
                      std::cout << shadowed << std::endl; //Prints 3
                      {
                          int shadowed = 4;
                          std::cout << ::shadowed << std::endl; //Prints 1
                          std::cout << this->shadowed << std::endl; //Prints 2
                          //It is not possible to print the argument version of shadowed
                          //here.
                          std::cout << shadowed << std::endl; //Prints 4
                      }
                  }
                  
                  int main()
                  {
                      Foo().bar();
                  }
                  

                  这篇关于在 C++ 中,阴影变量名称的范围解析(“优先顺序")是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么 GCC 允许在不首先使用其命名空间的情况下调用此函数? 下一篇:访问不同作用域的同名变量

                  相关文章

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

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

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

                      <bdo id='YWWU9'></bdo><ul id='YWWU9'></ul>