public member function
<memory>

std::shared_ptr::reset

(1)
void reset() noexcept;
(2)
template <class U> void reset (U* p);
(3)
template <class U, class D> void reset (U* p, D del);
(4)
template <class U, class D, class Alloc> void reset (U* p, D del, Alloc alloc);
Reset pointer
For signature (1) the object becomes empty (as if default-constructed).

In all other cases, the shared_ptr acquires ownership of p with a use count of 1, and -optionally- with del and/or alloc as deleter and allocator, respectively.

Additionally, a call to this function has the same side effects as if shared_ptr's destructor was called before its value changed (including the deletion of the managed object if this shared_ptr was unique).

Parameters

p
Pointer whose ownership is taken over by the object.
Generally, this pointer should not be already managed by any other managed pointer (i.e., this value should not come from calling member get on a managed pointer).
U* shall be implicitly convertible to T* (where T is shared_ptr's template parameter).
del
Deleter object to be used to release the owned object.
This shall be a callable object taking a pointer to T as argument for its functional call (where T is shared_ptr's template parameter).
alloc
Allocator object used to allocate/deallocate internal storage.

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// shared_ptr::reset example
#include <iostream>
#include <memory>

int main () {
  std::shared_ptr<int> sp;  // empty

  sp.reset (new int);       // takes ownership of pointer
  *sp=10;
  std::cout << *sp << '\n';

  sp.reset (new int);       // deletes managed object, acquires new pointer
  *sp=20;
  std::cout << *sp << '\n';

  sp.reset();               // deletes managed object

  return 0;
}


Output:
10
20

See also