Topic: Owning Pointers (was: Proposal: Improved `auto_ptr')


Author: fknauss@qualcomm.com (Friedrich Knauss)
Date: 1996/08/14
Raw View
Carlo Kid  <carlo@runaway.xs4all.nl> wrote:
>auto_ptr.h:
...
>template<class X>
>class auto_ptr {
...
>  template<class Y>
>  auto_ptr(const auto_ptr<X /*Y*/ >& r) :
>      owner(r.owner && !r.frozen), frozen(false),
>      px(r.frozen ? r.get() : r.release()) {}
> template<class Y>
>  auto_ptr& operator=(const auto_ptr<X /*Y*/ >& r) {
>     if ((void*)&r != (void*)this)
>     {
>       if (owner)
>         delete px;
>       owner = r.owner;
>       px = r.release();
>     }
>     return *this;
>   }
...
>}

Aside from the fact that the assignment operator doesn't check the frozen
state before releasing, I've been wondering about ownership pointers. The
common ones seem to be auto_ptr and count_ptr, but I've come up with a
a couple of variations that I could inherit from auto_ptr if auto_ptr was
meant to be inherited from. Since it's not, I can make my own ownership
pointer, which supports the same functionality. It seems then, however,
that I'd want to be able to transfer ownership from any one of these
ownership pointers to another via the release()  & owner() members. To
do this though, it seems that the copy constructor should then look like this:

...
template <class Y>
auto_ptr(Y &rhs):owner_(rhs.owner()),px(rhs.release()) {}
...

It seems to me that this would allow construction of an auto_ptr from
any class that supports the owner() and release() functions, including
an auto_ptr itself.

Am I barking up the wrong tree here, and if so how else do I achieve
what I want to do?

--
-- fritzz@qualcomm.com
-- Pain is temporary, glory is forever.
--


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]