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

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

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

      如何在 C++ 中通过引用返回类对象?

      How to return a class object by reference in C++?(如何在 C++ 中通过引用返回类对象?)
      <tfoot id='yIIU8'></tfoot>

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

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

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

            <tbody id='yIIU8'></tbody>
          <legend id='yIIU8'><style id='yIIU8'><dir id='yIIU8'><q id='yIIU8'></q></dir></style></legend>
              • 本文介绍了如何在 C++ 中通过引用返回类对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个名为 Object 的类,用于存储一些数据.

                I have a class called Object which stores some data.

                我想使用这样的函数通过引用返回它:

                I would like to return it by reference using a function like this:

                    Object& return_Object();
                

                然后,在我的代码中,我会这样称呼它:

                Then, in my code, I would call it like this:

                    Object myObject = return_Object();
                

                我已经编写了这样的代码并且它可以编译.但是,当我运行代码时,我始终遇到段错误.通过引用返回类对象的正确方法是什么?

                I have written code like this and it compiles. However, when I run the code, I consistently get a seg fault. What is the proper way to return a class object by reference?

                推荐答案

                您可能正在返回堆栈上的对象.也就是说,return_Object() 可能看起来像这样:

                You're probably returning an object that's on the stack. That is, return_Object() probably looks like this:

                Object& return_Object()
                {
                    Object object_to_return;
                    // ... do stuff ...
                
                    return object_to_return;
                }
                

                如果这是你正在做的事情,那你就不走运了 - object_to_return 已经超出范围并在 return_Object 的末尾被破坏,所以 myObject 指的是一个不存在的对象.您要么需要按值返回,要么返回在更广泛范围内声明的 Objectnew 到堆上.

                If this is what you're doing, you're out of luck - object_to_return has gone out of scope and been destructed at the end of return_Object, so myObject refers to a non-existent object. You either need to return by value, or return an Object declared in a wider scope or newed onto the heap.

                这篇关于如何在 C++ 中通过引用返回类对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                What is a dangling reference?(什么是悬空引用?)
                C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)
                  • <bdo id='g9O0h'></bdo><ul id='g9O0h'></ul>

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

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