哪位大神给小弟我调试下 auto_ptr 本来可以转移拥有权的但现在 (如下面q=p;应该输出q=42;p=NULL1的结果却不是 还有p是如何得到42)
哪位大神给我调试下 auto_ptr 本来可以转移拥有权的但现在 (如下面q=p;应该输出q=42;p=NULL1的结果却不是 还有p是怎么得到42)
#include<iostream>
//#include<memory>
using namespace std;
template<class _Ty>
class auto_ptr {
public:
typedef _Ty element_type;
explicit auto_ptr(_Ty *_P = 0) _THROW0()
: _Owns(_P != 0), _Ptr(_P) {}
auto_ptr(const auto_ptr<_Ty>& _Y) _THROW0()
: _Owns(_Y._Owns), _Ptr(_Y.release()) {}
auto_ptr<_Ty>& operator = (const auto_ptr<_Ty>& _Y) _THROW0()
{if (this != &_Y)
{if (_Ptr != _Y.get())
{if (_Owns)
delete _Ptr;
_Owns = _Y._Owns; }
else if (_Y._Owns)
_Owns = true;
_Ptr = _Y.release(); }
return (*this); }
~auto_ptr()
{if (_Owns)
delete _Ptr; }
_Ty& operator*() const _THROW0()
{return (*get()); }
_Ty *operator->() const _THROW0()
{return (get()); }
_Ty *get() const _THROW0()
{return (_Ptr); }
_Ty *release() const _THROW0()
{((auto_ptr<_Ty> *)this)->_Owns = false;
return (_Ptr); }
public:
bool _Owns;
_Ty *_Ptr;
};
template <class T>
ostream& operator << (ostream& strm,const auto_ptr<T>& p)
{
if(p.get() == NULL)
{
strm << "NULL1";
}
else
{
strm << *p;
}
return strm;
}
int main()
{
auto_ptr<int> p(new int(42));
auto_ptr<int> q;
cout << "after initialization:"<<endl;
cout << " p: " << p << endl;
cout << " q: " << q << endl;
q = p;
cout << " after assigning auto pointers:" << endl;
cout << " p: " << p << endl;
cout << " q: " << q <<endl;
*q += 13;
p = q;
cout << " P:" << p << endl;
cout << "q: " << q <<endl;
return 0;
}
如下面q=p;应该输出q=42;p=NULL1的结果却不是 还有p是怎么得到42 应该p._Ptr=42 p怎么的得到值的
auto_ptr<int> p(new int(42));这步给我从new的原始定义开始说下
------解决方案--------------------
#include<iostream>
//#include<memory>
using namespace std;
template<class _Ty>
class auto_ptr {
public:
typedef _Ty element_type;
explicit auto_ptr(_Ty *_P = 0) _THROW0()
: _Owns(_P != 0), _Ptr(_P) {}
auto_ptr(const auto_ptr<_Ty>& _Y) _THROW0()
: _Owns(_Y._Owns), _Ptr(_Y.release()) {}
auto_ptr<_Ty>& operator = (const auto_ptr<_Ty>& _Y) _THROW0()
{if (this != &_Y)
{if (_Ptr != _Y.get())
{if (_Owns)
delete _Ptr;
_Owns = _Y._Owns; }
else if (_Y._Owns)
_Owns = true;
_Ptr = _Y.release(); }
return (*this); }
~auto_ptr()
{if (_Owns)
delete _Ptr; }
_Ty& operator*() const _THROW0()
{return (*get()); }
_Ty *operator->() const _THROW0()
{return (get()); }
_Ty *get() const _THROW0()
{return (_Ptr); }
_Ty *release() const _THROW0()
{((auto_ptr<_Ty> *)this)->_Owns = false;
return (_Ptr); }
public:
bool _Owns;
_Ty *_Ptr;
};
template <class T>
ostream& operator << (ostream& strm,const auto_ptr<T>& p)
{
if(p.get() == NULL)
{
strm << "NULL1";
}
else
{
strm << *p;
}
return strm;
}
int main()
{
auto_ptr<int> p(new int(42));
auto_ptr<int> q;
cout << "after initialization:"<<endl;
cout << " p: " << p << endl;
cout << " q: " << q << endl;
q = p;
cout << " after assigning auto pointers:" << endl;
cout << " p: " << p << endl;
cout << " q: " << q <<endl;
*q += 13;
p = q;
cout << " P:" << p << endl;
cout << "q: " << q <<endl;
return 0;
}
如下面q=p;应该输出q=42;p=NULL1的结果却不是 还有p是怎么得到42 应该p._Ptr=42 p怎么的得到值的
auto_ptr<int> p(new int(42));这步给我从new的原始定义开始说下
------解决方案--------------------
#include<iostream>
using namespace std;
template<class _Ty>
class auto_ptr {
public:
typedef _Ty element_type;
explicit auto_ptr(_Ty *_P = 0) _THROW0(): _Owns(_P != 0), _Ptr(_P) {}
auto_ptr(const auto_ptr<_Ty>& _Y) _THROW0(): _Owns(_Y._Owns), _Ptr(_Y.release()) {}
auto_ptr<_Ty>& operator= (const auto_ptr<_Ty>& _Y) _THROW0()
{
if (this != &_Y)
{
if (_Ptr != _Y.get())
{
if (_Owns)
delete _Ptr;
_Owns = _Y._Owns;
}
else if (_Y._Owns)
_Owns = true;
_Ptr = _Y.release();
}
return (*this);
}
~auto_ptr()
{
if (_Owns)
delete _Ptr;
}
_Ty& operator*() const _THROW0()
{
return (*get());
}
_Ty *operator->() const _THROW0()
{
return (get());
}
_Ty *get() const _THROW0()
{
return (_Ptr);
}
_Ty *release() const _THROW0()
{
((auto_ptr<_Ty> *)this)->_Owns = false;
return (_Ptr);
}
public:
bool _Owns;
_Ty *_Ptr;
};
template <class T>
ostream& operator << (ostream& strm,const auto_ptr<T>& p)
{
if(p.get() == NULL)
{
strm << "NULL1";
}
else
{
strm << *p;
}
return strm;