public member function
<memory>

std::auto_ptr::release

X* release() throw();
Release pointer
Sets theauto_ptrinternal pointer to null pointer (which indicates it points to no object) without destructing the object currently pointed by theauto_ptr.

To force a destruction of the object pointed, use member function reset() instead.

The function returns a pointer to the object it pointed before the call, which is no longer its responsibility to destruct.

参数



返回值

A pointer to the element pointed by theauto_ptrobject before the call. After the call, the internal pointer's value is the null pointer (points to no object).
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
18
19
// auto_ptr::release example
#include <iostream>
#include <memory>

int main () {
  std::auto_ptr<int> auto_pointer (new int);
  int * manual_pointer;

  *auto_pointer=10;

  manual_pointer = auto_pointer.release();

  std::cout << "manual_pointer points to " << *manual_pointer << '\n';
  // (auto_pointer is now null-pointer auto_ptr)

  delete manual_pointer; 

  return 0;
}

输出

manual_pointer points to 10


另见