“const shared_ptr”和“shared_ptr”之间的区别?

[英]Difference between `const shared_ptr` and `shared_ptr`?


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

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

class Foo {
public:
    return_type getBar() const {
        return m_bar;
    }

private:
    boost::shared_ptr<Bar> m_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!

因此,为了支持getBar()的一致性,返回类型应该是boost: shared_ptr,防止对其指向的Bar进行修改。我的猜测是shared_ptr 是我想要返回的类型,而const shared_ptr 将防止指针本身重新分配到指向另一个Bar,但允许修改它指向的Bar…但是,我不确定。如果有人能证实这一点,或者如果我做错了就纠正我,我将不胜感激。谢谢!

3 个解决方案

#1


111  

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:

你是对的。要查看< const T > p;与const T * p相似;(或,等价地,T const * p;),即有指向的对象是const,而const shared_ptr p;与T* const p相似;也就是说p是const。总而言之:

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.

The same holds for weak_ptr and unique_ptr.

weak_ptr和unique_ptr也是如此。

#2


1  

boost::shared_ptr<Bar const> prevents modification of the Bar object through the shared pointer. As a return value, the const in boost::shared_ptr<Bar> const means that you cannot call a non-const function on the returned temporary; if it were for a real pointer (e.g. Bar* const), it would be completely ignored.

boost::shared_ptr 防止通过共享指针修改Bar对象。作为一个返回值,boost: shared_ptr const表示不能在返回的临时函数上调用非const函数;如果它是一个真正的指针(例如Bar* const),它将被完全忽略。

In general, even here, the usual rules apply: const modifies what precedes it: in boost::shared_ptr<Bar const>, the Bar; in boost::shared_ptr<Bar> const, it's the instantiation (the expression boost::shared_ptr<Bar> which is const.

一般来说,即使在这里,通常的规则也适用:const修改了它之前的内容::shared_ptr , Bar;在boost:::shared_ptr const,是实例化(表达式boost::shared_ptr , const)。

#3


1  

#Check this simple code to understand... copy-paste the below code to check on any c++11 compiler

#include <memory>
using namespace std;

class A {
    public:
        int a = 5;
};

shared_ptr<A> f1() {
    const shared_ptr<A> sA(new A);
    shared_ptr<A> sA2(new A);
    sA = sA2; // compile-error
    return sA;
}

shared_ptr<A> f2() {
    shared_ptr<const A> sA(new A);
    sA->a = 4; // compile-error
    return sA;
}

int main(int argc, char** argv) {
    f1();
    f2();
    return 0;
}
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2013/07/22/ecce58598cc7fb824e36a4cf685e6b7e.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告