public member function
<memory>

std::unique_ptr::reset

void reset (pointer p = pointer()) noexcept;
Reset pointer
Destroys the object currently managed by the unique_ptr (if any) and takes ownership of p.

If p is a null pointer (such as a default-initialized pointer), the unique_ptr becomes empty, managing no object after the call.

To release the ownership of the stored pointer without destroying it, use member function release instead.

The specialization of unique_ptr for array objects with runtime length (unique_ptr<T[],D>) does not accept pointers to types derived from element_type as argument. An additional signature is provided in this case for null pointers:
1
void reset (nullptr_t p) noexcept;

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).
pointer is a member type, defined as the pointer type that points to the type of object managed.

Return value

none

Example

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

int main () {
  std::unique_ptr<int> up;  // empty

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

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

  up.reset();               // deletes managed object

  return 0;
}

Output:
5
10


See also