Article View: comp.std.c++
Article #1937auto_ptr::reset and auto_ptr::operator=
From: 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. ]
Technical Details
Message-ID:
<smeyersDEn4DE.8vr@netcom.com>Path:
arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamageShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage From: smeyers@netcom.com (Scott Meyers) Newsgroups: comp.std.c++ Subject: auto_ptr::reset and auto_ptr::operator= Date: 9 Sep 1995 16:08:21 GMT Organization: NETCOM On-line Communication Services (408 261-4700 guest) Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <smeyersDEn4DE.8vr@netcom.com> NNTP-Posting-Host: taumet.eng.sun.com Content-Type: text Apparently-To: comp-std-c++@uunet.uu.net Content-Length: 1651 Originator: clamage@taumet