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

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

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

      返回 C++ 引用变量的做法是否邪恶?

      Is the practice of returning a C++ reference variable evil?(返回 C++ 引用变量的做法是否邪恶?)
      <tfoot id='f4PVp'></tfoot>
        <tbody id='f4PVp'></tbody>

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

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

                <legend id='f4PVp'><style id='f4PVp'><dir id='f4PVp'><q id='f4PVp'></q></dir></style></legend>
              • 本文介绍了返回 C++ 引用变量的做法是否邪恶?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                This is a little subjective I think; I'm not sure if the opinion will be unanimous (I've seen a lot of code snippets where references are returned).

                According to a comment toward this question I just asked, regarding initializing references, returning a reference can be evil because, [as I understand] it makes it easier to miss deleting it, which can lead to memory leaks.

                This worries me, as I have followed examples (unless I'm imagining things) and done this in a fair few places... Have I misunderstood? Is it evil? If so, just how evil?

                I feel that because of my mixed bag of pointers and references, combined with the fact that I'm new to C++, and total confusion over what to use when, my applications must be memory leak hell...

                Also, I understand that using smart/shared pointers is generally accepted as the best way to avoid memory leaks.

                解决方案

                In general, returning a reference is perfectly normal and happens all the time.

                If you mean:

                int& getInt() {
                    int i;
                    return i;  // DON'T DO THIS.
                }
                

                That is all sorts of evil. The stack-allocated i will go away and you are referring to nothing. This is also evil:

                int& getInt() {
                    int* i = new int;
                    return *i;  // DON'T DO THIS.
                }
                

                Because now the client has to eventually do the strange:

                int& myInt = getInt(); // note the &, we cannot lose this reference!
                delete &myInt;         // must delete...totally weird and  evil
                
                int oops = getInt(); 
                delete &oops; // undefined behavior, we're wrongly deleting a copy, not the original
                

                Note that rvalue references are still just references, so all the evil applications remain the same.

                If you want to allocate something that lives beyond the scope of the function, use a smart pointer (or in general, a container):

                std::unique_ptr<int> getInt() {
                    return std::make_unique<int>(0);
                }
                

                And now the client stores a smart pointer:

                std::unique_ptr<int> x = getInt();
                

                References are also okay for accessing things where you know the lifetime is being kept open on a higher-level, e.g.:

                struct immutableint {
                    immutableint(int i) : i_(i) {}
                
                    const int& get() const { return i_; }
                private:
                    int i_;
                };
                

                Here we know it's okay to return a reference to i_ because whatever is calling us manages the lifetime of the class instance, so i_ will live at least that long.

                And of course, there's nothing wrong with just:

                int getInt() {
                   return 0;
                }
                

                If the lifetime should be left up to the caller, and you're just computing the value.

                Summary: it's okay to return a reference if the lifetime of the object won't end after the call.

                这篇关于返回 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++ 引用发生变化)

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

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