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

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

        如何访问在另一个函数中的一个函数中定义和声明的变量?

        时间:2023-10-17

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

          <tfoot id='PdRLm'></tfoot>

          • <small id='PdRLm'></small><noframes id='PdRLm'>

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

                <bdo id='PdRLm'></bdo><ul id='PdRLm'></ul>
                  本文介绍了如何访问在另一个函数中的一个函数中定义和声明的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  谁能告诉我如何访问在另一个函数中的函数中声明和定义的变量.例如

                  Can anyone tell me how to access variables declared and defined in a function in another function. E.g

                  void function1()
                  {
                     string abc;
                  }
                  
                  void function2()
                  {
                     I want to access abc here.
                  }
                  

                  怎么做?我知道使用参数我们可以做到这一点,但还有其他方法吗?

                  How to do that? I know using parameters we can do that but is there any other way ?

                  推荐答案

                  C++的方式是通过引用你的函数来传递abc:

                  The C++ way is to pass abc by reference to your function:

                  void function1()
                  {
                      std::string abc;
                      function2(abc);
                  }
                  void function2(std::string &passed)
                  {
                      passed = "new string";
                  }
                  

                  您也可以将字符串作为指针传递并在函数 2 中取消引用它.这更像是 C 风格的做事方式,并不安全(例如,可以传入 NULL 指针,如果没有良好的错误检查,它将导致未定义的行为或崩溃.

                  You may also pass your string as a pointer and dereference it in function2. This is more the C-style way of doing things and is not as safe (e.g. a NULL pointer could be passed in, and without good error checking it will cause undefined behavior or crashes.

                  void function1()
                  {
                      std::string abc;
                      function2(&abc);
                  }
                  void function2(std::string *passed)
                  {
                      *passed = "new string";
                  }
                  

                  这篇关于如何访问在另一个函数中的一个函数中定义和声明的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么未命名的 C++ 对象会在作用域块结束之前销毁? 下一篇:混合外部和常量

                  相关文章

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

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

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