public member function
<memory>

std::auto_ptr::operator=

auto_ptr& operator= (auto_ptr& a) throw();template <class Y>  auto_ptr& operator= (auto_ptr<Y>& a) throw();auto_ptr& operator= (auto_ptr_ref<X> r) throw();
Release and copy auto_ptr
Copies the value of the pointer held by a (or r).

The object on the left-hand side takes ownership of the pointer (i.e., it is now in charge of freeing the memory block when destroyed). Therefore, the auto_ptr object on the right-hand side is automatically released (i.e., it is set to point to the null pointer) after the copy.

If the (left-hand side) object was being used to point to an object before the operation, the pointed object is destroyed (by calling operator delete).

Parameters

a
An auto_ptr object.
When the types held by the origin and destination auto_ptrs are different (second constructor version), an implicit conversion must exist between their pointer types.
r
An auto_ptr_ref object (a reference to auto_ptr).
X is auto_ptr's template parameter (i.e., the type pointed).

Return value

The left-hand side auto_ptr object (*this).

Example

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

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

  p = std::auto_ptr<int> (new int);

  *p = 11;

  p2 = p;

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

  return 0;
}

Output:

p2 points to 11


See also