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

  • <legend id='GQVEb'><style id='GQVEb'><dir id='GQVEb'><q id='GQVEb'></q></dir></style></legend>

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

        <tfoot id='GQVEb'></tfoot>

        访问不同作用域的同名变量

        Accessing variables with the same name at different scopes(访问不同作用域的同名变量)

        • <legend id='7wMxz'><style id='7wMxz'><dir id='7wMxz'><q id='7wMxz'></q></dir></style></legend>

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

          <small id='7wMxz'></small><noframes id='7wMxz'>

            <tfoot id='7wMxz'></tfoot>
              <tbody id='7wMxz'></tbody>

                <bdo id='7wMxz'></bdo><ul id='7wMxz'></ul>

                  本文介绍了访问不同作用域的同名变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  #include <iostream>
                  using namespace std;
                  
                          int a = 1;
                  
                  int main()
                  {
                          int a = 2;
                  
                          if(true)
                          {
                                  int a = 3;
                                  cout << a 
                                       << " " << ::a // Can I access a = 2 here?
                                       << " " << ::a << endl;
                          }
                          cout << a << " " << ::a << endl;
                  }
                  

                  有输出

                  3 1 1
                  2 1
                  

                  有没有办法在if语句中访问等于2的'a',其中'a'等于3,输出

                  Is there a way to access the 'a' equal to 2 inside the if statement where there is the 'a' equal to 3, with the output

                  3 2 1
                  2 1
                  

                  注意:我知道这不应该完成(并且代码不应该达到我需要询问的地步).这个问题更像是可以做到".

                  Note: I know this should not be done (and the code should not get to the point where I need to ask). This question is more "can it be done".

                  推荐答案

                  不,你不能,一个 (2) 是隐藏的.

                  No you can't, a (2) is hidden.

                  参考:3.3.7/1

                  名称可以被显式隐藏在同名声明中嵌套声明区域或派生区域类(10.2).

                  A name can be hidden by an explicit declaration of that same name in a nested declarative region or derived class (10.2).

                  参考:3.4.3/1

                  类或命名空间的名称成员可以在 :: 之后引用范围解析运算符 (5.1)应用于嵌套名称说明符指定它的类或命名空间.在查找前面的名称期间:: 范围解析运算符,对象、函数和枚举器名称被忽略.如果找到的名字不是类名(第 9 条)或namespace-name (7.3.1),程序是格式错误.

                  The name of a class or namespace member can be referred to after the :: scope resolution operator (5.1) applied to a nested-name-specifier that nominates its class or namespace. During the lookup for a name preceding the :: scope resolution operator, object, function, and enumerator names are ignored. If the name found is not a class-name (clause 9) or namespace-name (7.3.1), the program is ill-formed.

                  这篇关于访问不同作用域的同名变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)

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

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

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