I encounter a question with shared_ptr
, it seems that to initialize an empty shared_ptr
, there are a two common ways as quoted from std::shared_ptr: reset() vs. assignment
T {
T(X x, Y y);
~T();
}
shared_ptr<T> p;
p.reset(new T(x,y));
p = make_shared<T>(t1) //t1 is a T type object
However, why this won't work?
p = shared_ptr<T>(new T(x,y));
Is it just a bad practice or it is simply wrong? Thanks!
There is an answer saying that make_shared is more efficient than new in terms of construction Is make_shared really more efficient than new?:
std::shared_ptr<Object> p1 = std::make_shared<Object>("foo"); //copy??
std::shared_ptr<Object> p2(new Object("foo")); //conversion?