Thread View: comp.std.c++
1 messages
1 total messages
Started by Scott Meyers
Sat, 09 Sep 1995 16:08
auto_ptr::reset and auto_ptr::operator=
Author: Scott Meyers
Date: Sat, 09 Sep 1995 16:08
Date: Sat, 09 Sep 1995 16:08
67 lines
2444 bytes
2444 bytes
Thanks to many helpful readers of this newsgroup, I got my auto_ptr
template working. I think. The problem is, I'm still not sure what
"working" is. This is what the DWP has to say about the reset
function:
X* reset(X* p =0);
Requires:
p points to an object of class X or a class derived from X for which
delete p is defined and accessible, or else p is a null pointer
Postcondition:
get() == p
The postcondition is mildly helpful, but what are the semantics of this
function? What is the return value? Increasingly I believe this
function must delete the old value of the embedded pointer before giving
that pointer a new value. This makes sense in view of the DWP's
specification for operator=, which uses reset:
void operator=(auto_ptr& a);
Effects:
copies the argument a to *this.
Calls reset(a.release()).
Postcondition:
get() == the value returned from a.release().
What follows are my best guesses for the definitions of the operator=
and reset functions:
template<class T>
class auto_ptr {
public:
void operator=(auto_ptr<T>& rhs);
T * reset(T *p = 0);
...
private:
T *pointee;
};
template<class T>
inline void auto_ptr<T>::operator=(auto_ptr<T>& rhs)
{ if (this != &rhs) reset(rhs.release()); }
template<class T>
inline T* auto_ptr<T>::reset(T *p)
{
delete pointee;
return pointee = p;
}
Questions:
1. Does my implementation of reset give it the correct semantics?
How do you know? :-)
2. Why does operator= return void? This prevents chains of
assignments, but why should such chains be prohibited?
Thanks,
Scott
[ 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. ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads