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

      1. <small id='ELFiL'></small><noframes id='ELFiL'>

        有没有办法在 C++ 中访问外部作用域中的局部变量?

        Is there any way to access a local variable in outer scope in C++?(有没有办法在 C++ 中访问外部作用域中的局部变量?)

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

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

            1. <tfoot id='DjADa'></tfoot>

                <tbody id='DjADa'></tbody>
              <legend id='DjADa'><style id='DjADa'><dir id='DjADa'><q id='DjADa'></q></dir></style></legend>
                • <bdo id='DjADa'></bdo><ul id='DjADa'></ul>
                  本文介绍了有没有办法在 C++ 中访问外部作用域中的局部变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  只是出于好奇:如果我有嵌套的作用域,就像在这个示例 C++ 代码中一样

                  Just out of curiosity: if I have nested scopes, like in this sample C++ code

                  using namespace std;
                  
                  int v = 1; // global
                  
                  int main (void)
                  {
                      int v = 2; // local
                      {
                          int v = 3; // within subscope
                          cout << "subscope: " << v << endl;
                          // cout << "local: " << v << endl; 
                          cout << "global: " << ::v << endl;
                      }
                      cout << "local: " << v << endl;
                  
                      cout << "global: " << ::v << endl;
                  
                  }
                  

                  有没有办法从中间"范围(非全局或本地)访问变量 v 的值 2?

                  Is there any way to access the variable v with the value 2 from the "intermediate" scope (neither global nor local)?

                  推荐答案

                  你可以像这样声明一个新的引用作为别名

                  You can declare a new reference as an alias like so

                  int main (void)
                  {
                      int v = 2; // local 
                      int &vlocal = v;
                      {
                          int v = 3; // within subscope
                          cout << "local: " << vlocal  << endl; 
                      }
                  }
                  

                  但我会完全避免这种做法.我花了几个小时来调试这样的构造,因为在调试器中显示的变量因范围而改变,我无法弄清楚它是如何改变的.

                  这篇关于有没有办法在 C++ 中访问外部作用域中的局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                  Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                  STL BigInt class implementation(STL BigInt 类实现)
                  Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                  Move list element to the end in STL(在 STL 中将列表元素移动到末尾)
                  Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)
                    • <bdo id='gkN0k'></bdo><ul id='gkN0k'></ul>

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

                        <tbody id='gkN0k'></tbody>

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

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