Article View: comp.std.c++
Article #2881auto_ptr<X> questions
From: corfmanr
Date: Thu, 07 Dec 1995 18:35
Date: Thu, 07 Dec 1995 18:35
49 lines
1555 bytes
1555 bytes
I implemented a version of the auto_ptr<X> template class
for the interim until the compiler I use offers one.
I'm not sure how to protect against something like the
following:
auto_ptr<int> p(new int);
delete p.reset(p.get());
Now I know the above is stupid and I would hope nobody would
actually do it, but I think it is a good pedagogical example
of a problem that may occur. To overcome this problem, I
coded up the reset method as follows:
template<class X> class auto_ptr
{
X* ptr;
public:
...
};
template<class X>
X* auto_ptr<X>::reset(X* p)
{
if (p == ptr)
return 0;
X* pt = ptr;
ptr = p;
return pt;
}
I'd like to get some comments on if this is a reasonable and valid
way to implement the reset method. How has anyone else handled this
situation.
Thanks,
--
Russell Corfman
AG Communication Systems Internet: corfmanr@agcs.com
P.O. Box 52179 Voice: (602) 581-4403
Phoenix, AZ 85072-2179 Fax: (602) 582-7111
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<9512071009.ZM28651@velo>Path:
arc1-ca.pugleaf.net!news.mindlink.net!agate!agate!MattShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.mindlink.net!agate!agate!Matt From: corfmanr@agcs.com Newsgroups: comp.std.c++ Subject: auto_ptr<X> questions Date: 07 Dec 1995 18:35:21 GMT Organization: <none> Approved: Matt@Austern Message-ID: <9512071009.ZM28651@velo> NNTP-Posting-Host: austern.hip.berkeley.edu