// util/autoptr1.cpp #include #include using namespace std; /* Определение оператора вывода для auto_ptr * - вывод значения объекта или NULL */ template ostream& operator<< (ostream& strm, const auto_ptr& p) { // Указывает ли p на объект? if (p.get() == NULL) { strm << "NULL"; // НЕТ: вывести строку NULL } else { strm << *p; // ДА: вывести объект } return strm; } int main() { auto_ptr p(new int(42)); auto_ptr 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; // Модификация объекта, принадлежащего q p = q; cout << "after change and reassignment:" << endl; cout << " p: " << p << endl; cout << " q: " << q << endl; }