Dereferencing pointers can make the code very hard to read. What I usually do is putting a reference to the pointed object and working with the reference. Example:
shared_ptr<std::vector<int> > sp = get_sp_to_vector();
std::vector<int>& vec = *sp;
...
vec.push_back(5);
I wonder if it’s a good practice. Does it have any drawback?
Update: To complete the example, I’d define get_sp_to_vector() the following way:
shared_ptr<std::vector<int> > get_sp_to_vector()
{
// create a vector and send back a shared pointer pointing at it
shared_ptr<std::vector<int> > sp(new std::vector<int>);
sp->push_back(1); sp->push_back(3);
return sp;
}
Using local references is common, especially inside loop bodies, but there is one requirement: only do it when the reference will obviously live as long as the target object.
This usually isn’t hard to guarantee, but here are some bad examples: