I'd like to wrap raw pointer member to some smart pointer to prevent deleting inside a developing class. Owner of the object under pointer is outside of class. So, looks like boost::shared_ptr
and std::auto_ptr
does not fit. The following is a reduced example:
class Foo {
boost::weak_ptr<Bar> m_bar;
public:
void setBar(const Bar *bar) { // bar created on heap
m_bar = bar; // naturally compilation error
}
};
Of course, it induces compilation error. What is a correct way to initialize weak_ptr
from a raw pointer (if exist)?
You can't do that, you can only create a weak_ptr out of a shared_ptr or another weak_ptr. So the idea would be that the owner of the pointer hold a shared_ptr instead of a raw pointer, and everything should be ok.