#include<iostream>
#include<cstdlib>
using namespace std;
#define __STL_NOTHROW
template <class X> class auto_ptr {
private:
X* ptr;
mutable bool owns;
public:
void display()
{
std::cout<<"address:"<<(void *)ptr<<"\towns:"<<owns<<"\tvalue:";
if(ptr)
std::cout<<*ptr;
else
std::cout<<"none value";
//std::cout<<"\tis delete: "<<del;
std::cout<<"\n";
}
typedef X element_type;
explicit auto_ptr(X* p = 0) __STL_NOTHROW : ptr(p), owns(p) {}
auto_ptr(const auto_ptr& a) __STL_NOTHROW : ptr(a.ptr), owns(a.owns) {
a.owns = 0;
}
template <class T> auto_ptr(const auto_ptr<T>& a) __STL_NOTHROW
: ptr(a.ptr), owns(a.owns) {
a.owns = 0;
}
auto_ptr& operator=(const auto_ptr& a) __STL_NOTHROW {
if (&a != this) {
if (owns)
{
delete ptr;
}
owns = a.owns;
ptr = a.ptr;
a.owns = 0;
}
}
template <class T> auto_ptr& operator=(const auto_ptr<T>& a) __STL_NOTHROW {
if (&a != this) {
if (owns)
{
delete ptr;
}
owns = a.owns;
ptr = a.ptr;
a.owns = 0;
}
}
~auto_ptr() {
if (owns)
{
delete ptr;
}
}
X& operator*() const __STL_NOTHROW { return *ptr; }
X* operator->() const __STL_NOTHROW { return ptr; }
X* get() const __STL_NOTHROW { return ptr; }
X* release() const __STL_NOTHROW { owns = false; return ptr;}
};
template<class T>
void fun(auto_ptr<T> &p)
{
p.display();
auto_ptr<T> q(p);
q.display();
}
int main()
{
auto_ptr<int> p(new int(5));
p.display();
fun<int>(p);
p.display();
*p=6;
p.display();
return 0;
}
#include<cstdlib>
using namespace std;
#define __STL_NOTHROW
template <class X> class auto_ptr {
private:
X* ptr;
mutable bool owns;
public:
void display()
{
std::cout<<"address:"<<(void *)ptr<<"\towns:"<<owns<<"\tvalue:";
if(ptr)
std::cout<<*ptr;
else
std::cout<<"none value";
//std::cout<<"\tis delete: "<<del;
std::cout<<"\n";
}
typedef X element_type;
explicit auto_ptr(X* p = 0) __STL_NOTHROW : ptr(p), owns(p) {}
auto_ptr(const auto_ptr& a) __STL_NOTHROW : ptr(a.ptr), owns(a.owns) {
a.owns = 0;
}
template <class T> auto_ptr(const auto_ptr<T>& a) __STL_NOTHROW
: ptr(a.ptr), owns(a.owns) {
a.owns = 0;
}
auto_ptr& operator=(const auto_ptr& a) __STL_NOTHROW {
if (&a != this) {
if (owns)
{
delete ptr;
}
owns = a.owns;
ptr = a.ptr;
a.owns = 0;
}
}
template <class T> auto_ptr& operator=(const auto_ptr<T>& a) __STL_NOTHROW {
if (&a != this) {
if (owns)
{
delete ptr;
}
owns = a.owns;
ptr = a.ptr;
a.owns = 0;
}
}
~auto_ptr() {
if (owns)
{
delete ptr;
}
}
X& operator*() const __STL_NOTHROW { return *ptr; }
X* operator->() const __STL_NOTHROW { return ptr; }
X* get() const __STL_NOTHROW { return ptr; }
X* release() const __STL_NOTHROW { owns = false; return ptr;}
};
template<class T>
void fun(auto_ptr<T> &p)
{
p.display();
auto_ptr<T> q(p);
q.display();
}
int main()
{
auto_ptr<int> p(new int(5));
p.display();
fun<int>(p);
p.display();
*p=6;
p.display();
return 0;
}