I believe there is a typo in Stroustup's book, third edition page 368. Could someone confirm?

122 views Asked by At

I believe there's a typo on this code snippet extracted from Stroustup's book, at its page 368 :

template <class X> class std::auto_ptr
{
    template <class Y> struct auto_ptr_ref { /* ... */ }; // helper class
    X * ptr;
    public :
    typedef X element_type;
    explicit auto_ptr(X* p =0) throw() { ptr = 0; }
    auto_ptr (auto_ptr& a) throw() { ptr = a.ptr; a.ptr = 0; } // note: not const auto_ptr&

    /* ... */
};

Shouldn't

explicit auto_ptr(X* p =0) throw() { ptr = 0; }

be

explicit auto_ptr(X* p =0) throw() { ptr = p; }

instead ?

2

There are 2 answers

0
ta.speot.is On BEST ANSWER

The errata for the book makes some changes:

Chapter 14:

pg 367-368 A recent standards change modified the definition of auto_ptr. Please replace the last paragraph on page 367 and page 368 ...

2
Bugster On

I've looked in that book however I have a newer version, which has some extra pages.

Anyhow, that code looks correct because it's saying p = 0 and by saying ptr = p is the same thing with ptr = 0.