Reference

public member function
<memory>

std::auto_ptr::auto_ptr

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_ptr object. 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
// auto_ptr example
#include <iostream>
#include <memory>
int main () {
 std::auto_ptr<int> p1 (new int);
 *p1.get()=10;
 std::auto_ptr<int> p2 (p1);
 std::cout << "p2 points to " << *p2 << '\n';
 // (p1 is now null-pointer auto_ptr)
 return 0;
}

Output:

p2 points to 10


See also

auto_ptr::operator=
Release and copy auto_ptr (public member function)
auto_ptr::reset
Deallocate object pointed and set new value (public member function)
auto_ptr::~auto_ptr
Destroy auto_ptr (public member function)

AltStyle によって変換されたページ (->オリジナル) /