Auto ptrIn the C++ programming language, auto_ptr is an obsolete smart pointer class template that was available in previous versions of the C++ standard library (declared in the The The characteristics of The C++11 standard made DeclarationThe namespace std {
template <class Y>
struct auto_ptr_ref {};
template <class X>
class auto_ptr {
public:
using element_type = X;
// 20.4.5.1 construct/copy/destroy:
explicit auto_ptr(X* p = 0) throw();
auto_ptr(auto_ptr&) throw();
template <class Y>
auto_ptr(auto_ptr<Y>&) throw();
auto_ptr& operator=(auto_ptr&) throw();
template <class Y>
auto_ptr& operator=(auto_ptr<Y>&) throw();
auto_ptr& operator=(auto_ptr_ref<X>) throw();
~auto_ptr() throw();
// 20.4.5.2 members:
X& operator*() const throw();
X* operator->() const throw();
X* get() const throw();
X* release() throw();
void reset(X* p = 0) throw();
// 20.4.5.3 conversions:
auto_ptr(auto_ptr_ref<X>) throw();
template <class Y>
operator auto_ptr_ref<Y>() throw();
template <class Y>
operator auto_ptr<Y>() throw();
};
}
SemanticsThe #include <iostream>
#include <memory>
using std::auto_ptr;
int main(int argc, char* argv[]) {
int* a = new int[10];
auto_ptr<int[]> x(a);
auto_ptr<int[]> y;
y = x;
std::cout << x.get() << std::endl; // Print NULL
std::cout << y.get() << std::endl; // Print non-NULL address of a
return 0;
}
This code will print a NULL address for the first Notice that the object pointed by an Because of its copy semantics, See alsoReferences
External links
|