<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++ 引用变量的做法是否邪恶?

      时间:2023-12-03
      <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++ 引用变量的做法是否邪恶?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:树莓派 i2c 读/写错误 下一篇:有没有办法找到引用的地址?

                相关文章

                <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>
                    <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>

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