I am currently following the book **Effective Modern C++" and it says
Avoid creating std::shared_ptrs from variables of raw pointer type.
And I am convinced with the explanation so that I, too, agree on that we need to avoid. But there is an exception I encountered.
class Person
{
protected:
Person();
public:
static std::shared_ptr<Person> getShared()
{
return std::shared_ptr<Person>(new Person());
}
When we hide the default constructor std::make_shared
cannot do its job. That's why I use a static method in the example above. My question is
- Is this best I can do about the situation?
- I still use raw pointer to create a
shared_ptr
, but in this case I can predict what may happen to this pointer. Does this practice still threaten my code?