🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1949

auto_ptr::reset and auto_ptr::operator=

#1949
From: veldhuis
Date: Tue, 12 Sep 1995 01:41
52 lines
2037 bytes
:>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:
Sorry, I have no answers for that. I do have another question/remark.
The way the auto_ptr assignment is setup does have very different semantics 
from all other assignments I know in that the previous value becomes invalid 
after the assignment. Assume :

auto_ptr<int> a, b;

*a = 10;  // Set a to 10.
b = a;    // Set b also to a (10), but deletes a in the process ?
cout << *a;  // crash ??? since a is no longer valid ?


It seems to me that in the assignment a deep copy should be performed in 
stead of a binary copy.

I could be wrong when auto_ptr::release() does only set a flag that its 
pointer still is valid, but it is no longer the owner of the memory pointed 
to. In this case however the implementation of reset would be erroneous since 
that would have to test that flag also. The only correct way of implementing 
this that I can think of would be to use reference counting on the 
pointee. 

---
[ 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: <199509111753.AA15077@xs1.xs4all.nl>
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
Date: 12 Sep 95 01:41:38 GMT
Approved: fjh@cs.mu.oz.au
Organization: -
Newsgroups: comp.std.c++
From: veldhuis@xs4all.nl
X-Original-Date: Mon, 11 Sep 1995 19:53:10 +0200
Message-ID: <199509111753.AA15077@xs1.xs4all.nl>
Subject: auto_ptr::reset and auto_ptr::operator=