Reputation: 3388
I run into this piece of code:
const std::shared_ptr<const T>& a_shared_pointer,
I am really curious what do the two const mean? Do they mean the same thing? a_shared_pointer is a shared pointer that points to something we can't modify?
Upvotes: 0
Views: 271
Reputation: 69864
A shared pointer is analgous to a raw pointer in terms of dereferencing and constness.
Note that below, the term [const]
means we can choose whether to insert const or not:
[const] X * [const] p;
X* p;
- p is a mutable pointer (it can be changed to point at a different X) to a mutable X
const X *p
- p is a mutable pointer to an immutable X
X * const p;
- p is an immutable pointer (can only point to this X) to a mutable X
const X * const p;
- p is an immutable pointer (can only point to this X) to an immutable X
std::shared_ptr<T> &p
- reference to a mutable pointer to a mutable T
const std::shared_ptr<T> &p
- reference to an immutable pointer to a mutable T
std::shared_ptr<const T> &p
- reference to a mutable pointer to an immutable T
const std::shared_ptr<const T> &p
- reference to an immutable pointer to an immutable T
Upvotes: 3
Reputation: 976
a_shared pointer is a reference to a const std::shared_ptr
(e.g. you can't call not const operations on the pointer through this reference, like reset
etc.), pointing at a const T
meaning that the pointed to object is also const (so again, you can't do a_shared_ptr->non_const_method()
).
Upvotes: 1