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

      • <bdo id='RUwmp'></bdo><ul id='RUwmp'></ul>

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

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

        C++:非临时常量引用

        C++: non-temporary const reference(C++:非临时常量引用)
        <tfoot id='UbiWW'></tfoot>
            <tbody id='UbiWW'></tbody>
          • <legend id='UbiWW'><style id='UbiWW'><dir id='UbiWW'><q id='UbiWW'></q></dir></style></legend>

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

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

                  <i id='UbiWW'><tr id='UbiWW'><dt id='UbiWW'><q id='UbiWW'><span id='UbiWW'><b id='UbiWW'><form id='UbiWW'><ins id='UbiWW'></ins><ul id='UbiWW'></ul><sub id='UbiWW'></sub></form><legend id='UbiWW'></legend><bdo id='UbiWW'><pre id='UbiWW'><center id='UbiWW'></center></pre></bdo></b><th id='UbiWW'></th></span></q></dt></tr></i><div id='UbiWW'><tfoot id='UbiWW'></tfoot><dl id='UbiWW'><fieldset id='UbiWW'></fieldset></dl></div>
                • 本文介绍了C++:非临时常量引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要编写一个类,其构造函数接受一个对象的常量引用并将其存储在本地.

                  I need to write a class whose constructor takes a constant reference to a object and stores it locally.

                  为了避免我能预见的最常见的错误,我只想接受对非临时的引用(即:对左值的引用).

                  In order to avoid most common mistakes I can foresee, I'd like to only accept references to non-temporary (ie: references to lvalues).

                  如何编写一个只对非临时对象进行常量引用的函数?

                  How can I write a function that takes constant references to non-temporary only?

                  当然,即使是非临时的也可能超出范围,从而破坏我的类行为,但我相信通过禁止临时引用,我将避免大多数错误.

                  Of course even a non-temporary could go out of scope and thus break my class behavior, but I believe that by disallowing temporary references I will avoid most mistakes.

                  推荐答案

                  如果你要存储一个引用并且需要在构造函数完成后使用它,那么构造函数最好带一个指针:

                  If you are going to store a reference and need to use it after the constructor has completed, it's probably best for the constructor to take a pointer:

                  struct C {
                      C(const X* p) : p_(p) { }
                  
                      const X* p_;
                  };
                  

                  这样,几乎可以保证你不会有一个指向临时的指针(除非 X 做了一些非常愚蠢的事情,比如重载一元 & 到返回this).

                  This way, it's pretty much guaranteed that you won't have a pointer to a temporary (unless X does something really dumb, like overloading the unary & to return this).

                  如果构造函数接受一个指针,类的用户也更清楚他们需要注意他们传递给构造函数的 X 对象的生命周期.

                  If the constructor takes a pointer, it's also clearer to users of the class that they need to pay attention to the lifetime of the X object they pass to the constructor.

                  这篇关于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='fnoZV'><tr id='fnoZV'><dt id='fnoZV'><q id='fnoZV'><span id='fnoZV'><b id='fnoZV'><form id='fnoZV'><ins id='fnoZV'></ins><ul id='fnoZV'></ul><sub id='fnoZV'></sub></form><legend id='fnoZV'></legend><bdo id='fnoZV'><pre id='fnoZV'><center id='fnoZV'></center></pre></bdo></b><th id='fnoZV'></th></span></q></dt></tr></i><div id='fnoZV'><tfoot id='fnoZV'></tfoot><dl id='fnoZV'><fieldset id='fnoZV'></fieldset></dl></div>

                    <tfoot id='fnoZV'></tfoot>

                    • <bdo id='fnoZV'></bdo><ul id='fnoZV'></ul>
                        <tbody id='fnoZV'></tbody>
                        • <small id='fnoZV'></small><noframes id='fnoZV'>

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