This site states on "Ownership, Sources, and Sinks" :
"When you copy an auto_ptr
, you automatically transfer ownership from the source auto_ptr
to the target auto_ptr
; if the target auto_ptr
already owns an object, that object is first freed. After the copy, only the target auto_ptr
owns the pointer and will delete it in due time, while the source is set back to a null state and can no longer be used to refer to the owned object.".
Now consider the definition of operator=()
for the templacte<classX> class auto_ptr
, in Chapter 14, page 368 of Stroustrup's The C++ Programming Language Third Edition:
auto_ptr& operator=(auto_ptr& a) throw() { ptr = a.ptr; a.ptr = 0; }
I can't see the operator freeing the object addressed by ptr
, in case ptr != 0
!
operator=
isn't returning anything. – Londonerptr
? Is thatclassX*
orauto_ptr_guts<classX>
? If it's the latter, the code above would be fairly trivial. – Cousins