kyb
kyb

Reputation: 8141

Unreference shared_ptr

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++

Upvotes: 1

Views: 104

Answers (1)

eerorika
eerorika

Reputation: 238351

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.

Upvotes: 1

Related Questions