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

StackOverflow https://stackoverflow.com/questions/8961211

  •  18-04-2021
  •  | 
  •  

سؤال

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 ?

هل كانت مفيدة؟

المحلول

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 ...

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top