Difference between `const shared_ptr<T>` and `shared_ptr<const T>`?
C++BoostConstantsShared PtrC++ Problem Overview
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;
}
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!
C++ Solutions
Solution 1 - C++
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.
The same holds for weak_ptr
and unique_ptr
.
Solution 2 - C++
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.
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.
Solution 3 - C++
#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;
}
Solution 4 - C++
I would like to a simple demostration based on @Cassio Neri's answer:
#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
}