auto_ptr clarification and josuttis book

L

lallous

Hello

@ Page 48, the sample util/autoptr1.cpp:

/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <iostream>
#include <memory>
using namespace std;

/* define output operator for auto_ptr
* - print object value or NULL
*/
template <class T>
ostream& operator<< (ostream& strm, const auto_ptr<T>& p)
{
// does p own an object ?
if (p.get() == NULL) {
strm << "NULL"; // NO: print NULL
}
else {
strm << *p; // YES: print the object
}
return strm;
}


int main()
{
auto_ptr<int> p(new int(42));
auto_ptr<int> q;

cout << "after initialization:" << endl;
cout << " p: " << p << endl;
cout << " q: " << q << endl;

q = p;
cout << "after assigning auto pointers:" << endl;
cout << " p: " << p << endl;
cout << " q: " << q << endl;

*q += 13; // change value of the object q owns
p = q;
cout << "after change and reassignment:" << endl;
cout << " p: " << p << endl;
cout << " q: " << q << endl;

return 0;
}

The output as shown in the book is:
after initialization:
p: 42
q: NULL
after assigning auto pointers:
p: NULL
q: 42
after change and reassignment:
p: 55
q: NULL


However when I run in VC++ 6, the output is this:
after initialization:
p: 42
q: NULL
after assigning auto pointers:
p: 42
q: 42
after change and reassignment:
p: 55
q: 55


It seems that even if p transfered ownership to q, p.get() does not have
NULL! as if now p and q still refer to the int* however only 'q' will
destroy it.
Should p.get() return null so that one can know that 'p' owns nothing?
 
S

Sharad Kala

lallous said:
Hello

@ Page 48, the sample util/autoptr1.cpp:

/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <iostream>
#include <memory>
using namespace std;

/* define output operator for auto_ptr
* - print object value or NULL
*/
template <class T>
ostream& operator<< (ostream& strm, const auto_ptr<T>& p)
{
// does p own an object ?
if (p.get() == NULL) {
strm << "NULL"; // NO: print NULL
}
else {
strm << *p; // YES: print the object
}
return strm;
}


int main()
{
auto_ptr<int> p(new int(42));
auto_ptr<int> q;

cout << "after initialization:" << endl;
cout << " p: " << p << endl;
cout << " q: " << q << endl;

q = p;
cout << "after assigning auto pointers:" << endl;
cout << " p: " << p << endl;
cout << " q: " << q << endl;

*q += 13; // change value of the object q owns
p = q;
cout << "after change and reassignment:" << endl;
cout << " p: " << p << endl;
cout << " q: " << q << endl;

return 0;
}

The output as shown in the book is:
after initialization:
p: 42
q: NULL
after assigning auto pointers:
p: NULL
q: 42
after change and reassignment:
p: 55
q: NULL


However when I run in VC++ 6, the output is this:
after initialization:
p: 42
q: NULL
after assigning auto pointers:
p: 42
q: 42
after change and reassignment:
p: 55
q: 55


It seems that even if p transfered ownership to q, p.get() does not have
NULL! as if now p and q still refer to the int* however only 'q' will
destroy it.
Should p.get() return null so that one can know that 'p' owns nothing?

The problem is that you are using a (now) pretty old compiler.
The program just runs as expected on VC 7.
IIRC, Josuttis does mention that not all compilers had correct auto_ptr
support while he was writing the book.

-Sharad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top