Topic: auto_ptr<X> questions


Author: corfmanr@agcs.com
Date: 1995/12/07
Raw View
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. ]