0

What is the difference between:

std::shared_ptr<int> p1 = std::shared_ptr<int>(new int);

and

std::shared_ptr<int> p2 = (std::shared_ptr<int>) new int;

Which is better and why?

0

1 Answer 1

11

Neither. This one is strictly preferable:

auto p3 = std::make_shared<int>();

(Although it has slightly different semantics, since it initializes the int object, unlike your code.)

This version is subexpression-wise correct, doesn't contain the red-flag word "new", and also uses a more efficient allocation scheme.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.