C++ `const shared_ptr<T>` 和 `shared_ptr<const T>` 之间的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17793333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between `const shared_ptr<T>` and `shared_ptr<const T>`?
提问by Dave Lillethun
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 guessis 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 Bar>
是我想要返回的类型,而const shared_ptr<Bar>
会阻止将指针本身重新分配为指向不同的Bar
但允许修改Bar
它指向的...但是,我不确定。如果有确切知道的人可以确认这一点,或者如果我弄错了,请纠正我,我将不胜感激。谢谢!
回答by Cassio Neri
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<const T> p;
类似于const T * p;
(或等效地,T const * p;
),也就是说,指向的对象是const
而const shared_ptr<T> 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
。
回答by James Kanze
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 const>
防止Bar
通过共享指针修改
对象。作为返回值,const inboost::shared_ptr<Bar> 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
修改它之前的内容: in boost::shared_ptr<Bar const>
, the Bar
; in boost::shared_ptr<Bar> const
,它是实例化(boost::shared_ptr<Bar>
const的表达式。
回答by vivek2k6
#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;
}
回答by Jónás Balázs
I would like to a simple demostration based on @Cassio Neri's answer:
我想根据@Cassio Neri 的回答做一个简单的演示:
#include <memory>
int main(){
std::shared_ptr<int> i = std::make_shared<int>(1);
std::shared_ptr<int const> ci;
// i = ci; // compile error
ci = i;
std::cout << *i << "\t" << *ci << std::endl; // both will be 1
*i = 2;
std::cout << *i << "\t" << *ci << std::endl; // both will be 2
i = std::make_shared<int>(3);
std::cout << *i << "\t" << *ci << std::endl; // only *i has changed
// *ci = 20; // compile error
ci = std::make_shared<int>(5);
std::cout << *i << "\t" << *ci << std::endl; // only *ci has changed
}