I have a few containers in a class, for example, vector or map which contain shared_ptr's to objects living on the heap.
For example
template <typename T>
class MyExample
{
public:
private:
vector<tr1::shared_ptr<T> > vec;
map<tr1::shared_ptr<T> , int> h;
};
I want to have a public interface of this class that sometimes returns shared_ptrs to const objects (via shared_ptr) and sometimes shared_ptr where I allow the caller to mutate the objects. I want logical const correctness, so if I mark a method as const, it cannot change the objects on the heap.
Questions:
1) I am confused by the interchangeability of tr1::shared_ptr<const T>
and tr1::shared_ptr<T>
.
When someone passes a shared_ptr<const T>
shared_ptr into the class, do I store it as a shared_ptr<T>
or shared_ptr<const T>
inside the vector and map or do I change the map, vector types (e.g. insert_elemeent(shared_ptr<const T>
obj) ?
2) Is it better to instantiate classes as follows: MyExample<const int>
? That seems
unduly restrictive, because I can never return a shared_ptr<int>
?