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

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

      <tfoot id='sEHVU'></tfoot>

        • <bdo id='sEHVU'></bdo><ul id='sEHVU'></ul>
        <legend id='sEHVU'><style id='sEHVU'><dir id='sEHVU'><q id='sEHVU'></q></dir></style></legend>
      1. C++ 中的作用域和返回值

        Scope and return values in C++(C++ 中的作用域和返回值)

          <tbody id='BRgve'></tbody>

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

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

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

                • 本文介绍了C++ 中的作用域和返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我重新开始使用 C++ 并且正在考虑变量的范围.如果我在函数内部有一个变量,然后我返回该变量,那么该变量在返回时不会死",因为它所在的作用域已经结束?

                  I am starting again with c++ and was thinking about the scope of variables. If I have a variable inside a function and then I return that variable will the variable not be "dead" when it's returned because the scope it was in has ended?

                  我用一个返回字符串的函数尝试了这个,它确实有效.谁能解释一下?或者至少给我指出一些可以向我解释这一点的地方.

                  I have tried this with a function returning a string and it did work. Can anyone explain this? Or at least point me to some place that can explain this to me please.

                  谢谢

                  推荐答案

                  当函数终止时,发生以下步骤:

                  When the function terminates, the following steps happen:

                  • 函数的返回值是复制到占位符中为此目的放在堆栈上.

                  • The function’s return value is copied into the placeholder that was put on the stack for this purpose.

                  栈帧之后的一切指针弹出.这破坏所有局部变量和参数.

                  Everything after the stack frame pointer is popped off. This destroys all local variables and arguments.

                  返回值弹出堆栈并被分配为值的功能.如果该值功能没有分配给任何东西,没有分配发生,并且价值丢失了.

                  The return value is popped off the stack and is assigned as the value of the function. If the value of the function isn’t assigned to anything, no assignment takes place, and the value is lost.

                  下一条指令的地址执行从堆栈中弹出,并且 CPU 在那个指令.

                  The address of the next instruction to execute is popped off the stack, and the CPU resumes execution at that instruction.

                  栈和堆

                  这篇关于C++ 中的作用域和返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 作为模板参数时出错)
                  <tfoot id='8fOku'></tfoot>

                    <tbody id='8fOku'></tbody>

                    • <small id='8fOku'></small><noframes id='8fOku'>

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

                          <bdo id='8fOku'></bdo><ul id='8fOku'></ul>
                        • <legend id='8fOku'><style id='8fOku'><dir id='8fOku'><q id='8fOku'></q></dir></style></legend>