//展示const auto_ptr的特性
#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()
{
const auto_ptr<int> p(new int(42));
const auto_ptr<int> q(new int (0));
const auto_ptr<int> r;
cout << "after initialization:" << endl;
cout << " p: " << p << endl;
cout << " q: " << q << endl;
cout << " r: " << r << endl;
*q = *p;
//*r = *p; // ERROR: undefined behavior 对于一个“未指向任何对象”的auto_ptr进行提领(dereference)操作,C++标准规格,
//这会导致未定义行为,比较如说导致程序的崩溃。
*p = -77;
cout << "after assigning values:" << endl;
cout << " p: " << p << endl;
cout << " q: " << q << endl;
cout << " r: " << r << endl;
//q = p; // ERROR at compile time
//r = p; // ERROR at compile time
}
输出结果:
after initialization:
p: 42
q: 0
r: NULL
after assigning values:
p: -77
q: 42
r: NULL
请按任意键继续. . .