public member function
<memory>

std::auto_ptr::reset

void reset (X* p=0) throw();
Deallocate object pointed and set new value
Destructs the object pointed by theauto_ptrobject, if any, and deallocates its memory (by callingoperator delete). If a value for p is specified, the internal pointer is initialized to that value (otherwise it is set to the null pointer).

To only release the ownership of a pointer without destructing the object pointed by it, use member function release instead.

参数

p
Pointer to element. Its value is set as the new internal pointer's value for theauto_ptrobject.
X isauto_ptr's template parameter (i.e., the type pointed).

返回值



示例

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

int main () {
  std::auto_ptr<int> p;

  p.reset (new int);
  *p=5;
  std::cout << *p << '\n';

  p.reset (new int);
  *p=10;
  std::cout << *p << '\n';

  return 0;
}

输出

5
10


另见