Quick A: Because it's more efficient, since it can eliminate an additional allocation.
Recently on SO:
Difference in make_shared and normal shared_ptr in C++
std::shared_ptr<Object> p1 = std::make_shared<Object>("foo"); std::shared_ptr<Object> p2(new Object("foo"));Many google and stackoverflow posts are there on this, but I am not able to understand why
make_shared
is more efficient than directly usingshared_ptr
. Can someone explain me step by step sequence of objects created and operations done by both so that I will be able to understand howmake_shared
is efficient. I have given one example above for reference.
Add a Comment
Comments are closed.