<tfoot id='rhJUV'></tfoot>

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

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

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

        `const shared_ptr<T>`和`shared_ptr<const T>`之间的区别

        时间:2023-07-18
      2. <small id='PR4hk'></small><noframes id='PR4hk'>

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

                1. 本文介绍了`const shared_ptr<T>`和`shared_ptr<const T>`之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在为 C++ 中的共享指针编写一个访问器方法,它是这样的:

                  I'm writing an accessor method for a shared pointer in C++ that goes something like this:

                  class Foo {
                  public:
                      return_type getBar() const {
                          return m_bar;
                      }
                  
                  private:
                      boost::shared_ptr<Bar> m_bar;
                  }
                  

                  所以为了支持 getBar() 的常量性,返回类型应该是一个 boost::shared_ptr 以防止修改 Bar> 它指向.我的猜测shared_ptr 是我想要返回的类型,而 const shared_ptr 会阻止重新分配指针本身指向不同的 Bar 但允许修改它指向的 Bar... 但是,我不确定.如果知道的人可以确认这一点,或者如果我弄错了,我将不胜感激.谢谢!

                  So to support the const-ness of getBar() the return type should be a boost::shared_ptr that prevents modification of the Bar it points to. My guess is that shared_ptr<const Bar> is the type I want to return to do that, whereas const shared_ptr<Bar> would prevent reassignment of the pointer itself to point to a different Bar but allow modification of the Bar that it points to... However, I'm not sure. I'd appreciate it if someone who knows for sure could either confirm this, or correct me if I got it wrong. Thanks!

                  推荐答案

                  你说得对.shared_ptrp; 类似于const T * p;(或等价于T const * p;),即指向的对象是constconst shared_ptr;p;T* const p; 类似,这意味着 pconst.总结:

                  You are right. shared_ptr<const T> p; is similar to const T * p; (or, equivalently, T const * p;), that is, the pointed object is const whereas const shared_ptr<T> p; is similar to T* const p; which means that p is const. In summary:

                  shared_ptr<T> p;             ---> T * p;                                    : nothing is const
                  const shared_ptr<T> p;       ---> T * const p;                              : p is const
                  shared_ptr<const T> p;       ---> const T * p;       <=> T const * p;       : *p is const
                  const shared_ptr<const T> p; ---> const T * const p; <=> T const * const p; : p and *p are const.
                  

                  同样适用于 weak_ptrunique_ptr.

                  这篇关于`const shared_ptr<T>`和`shared_ptr<const T>`之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:适用于 64 位 Windows 的 64 位版本的 Boost 下一篇:删除与删除[]

                  相关文章

                2. <tfoot id='zswpN'></tfoot>
                3. <legend id='zswpN'><style id='zswpN'><dir id='zswpN'><q id='zswpN'></q></dir></style></legend>
                4. <small id='zswpN'></small><noframes id='zswpN'>

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

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