1

I know 2 ways:

// Declared somewhere but not on stack
shared_ptr<X> xptr = make_shared<X>();

xptr = nullptr;  //#1
xptr.reset();  //#2

As for me #1 looks better, but what is better from the point of C++

1 Answer 1

1

Neither is objectively better. In almost every case, the difference is purely aesthetic.

The only exception that I can think of is writing a generic function template that works with both smart, and bare pointers. only ptr = nullptr is valid syntax for bare pointers.

The assignment and reset member function differ in behaviour only when the pointer is not null.

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.