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:
See also
auto_ptr::reset | Deallocate object pointed and set new value (public member function) |
|