Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
complex
exception
functional
iterator
limits
locale
memory
new
numeric
stdexcept
typeinfo
utility
valarray
memory
classes:
allocator
auto_ptr
auto_ptr_ref
raw_storage_iterator
functions:
get_temporary_buffer
return_temporary_buffer
uninitialized_copy
uninitialized_fill
uninitialized_fill_n
auto_ptr
auto_ptr::auto_ptr
auto_ptr::~auto_ptr
member functions:
auto_ptr::get
auto_ptr::operator*
auto_ptr::operator->
auto_ptr::operator=
auto_ptr::operator
auto_ptr::release
auto_ptr::reset


auto_ptr::auto_ptr

public member function
explicit auto_ptr (X* p=0) throw();
auto_ptr (auto_ptr& a) throw();
template<class Y>
  auto_ptr (auto_ptr<Y>& a) throw();
auto_ptr (auto_ptr_ref<X> r) throw();

Construct auto_ptr object

Constructs an auto_ptr object either from a pointer or from another auto_ptr object.

Since auto_ptr objects take ownership of the pointer they point to, when a new auto_ptr is constructed from another auto_ptr, the former owner releases it.

Parameters

p
Pointer to an object of type X, which is the auto_ptr's template parameter.
If this parameter is 0 the auto_ptr is a null pointer (points to nowhere).
a
An auto_ptrobject. Ownership is taken from it, therefore, a releases it.
When the types held by the origin and destination auto_ptrs are different, an implicit conversion must be available between their pointers.
r
An auto_ptr_ref object (a reference to auto_ptr).
X is auto_ptr's template parameter (i.e., the type pointed).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// auto_ptr example
#include <iostream>
#include <memory>
using namespace std;
int main () {
  auto_ptr<int> p1 (new int);
  *p1.get()=10;
  auto_ptr<int> p2 (p1);
  cout << "p2 points to " << *p2 << "\n";
  // (p1 is now null-pointer auto_ptr)
  return 0;
}


Output:

p2 points to 10

See also