4、使用auto_ptr对象
auto_ptr类定义了解引用操作符(*)和箭头操作符(->)的重载版本,因为auto_ptr定义了这些操作符,所以可以用类似于使用内置指针的方式使用auto_ptr对象:
auto_ptrstrPtr(new string("HELLO!")); cout << *strPtr << endl; *strPtr = "TRex"; string s = *strPtr; cout << s << endl; if (strPtr -> empty()) { cout << "Empty!" << endl; } else { cout << "Not Empty!" << endl; }
auto_ptr的主要目的,在保证自动删除auto_ptr对象引用的对象的同时,支持普通指针式行为。正如我们所见,自动删除该对象这一事实导致在怎样复制和访问它们的地址值方面,auto_ptrs与普通指针明显不同。