1 Replies - 1570 Views - Last Post: 11 February 2012 - 03:01 AM

#1 Xing   User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

auto_ptr ownership issues

Posted 25 September 2007 - 09:06 PM

Description:
#include <iostream>
#include <memory>

using namespace std;

template <class T>
ostream& operator<< (ostream& stream, const auto_ptr<T>& ptr)
{
    //does ptr own an object ?
    if (ptr.get() == NULL)
    {
        stream << "NULL";        //NO: print NULL
    }
    else
    {
        stream << *ptr;           //YES: print the object
    }
    return stream;
}
int main()
{
    auto_ptr<int> ptr1(new int(100));
    auto_ptr<int> ptr2;
    cout << "after initialization:" << endl;
    cout << " ptr1: " << ptr1 << endl;
    cout << " ptr2: " << ptr2 << endl;
    ptr2 = ptr1;

    cout << "after assigning auto pointers:" << endl;
    cout << " ptr1: " << ptr1 << endl;
    cout << " ptr2: " << ptr2 << endl;
    *ptr2 += 15;
    ptr1 = ptr2;
    cout << "after change and reassignment:" << endl;
    cout << " ptr1: " << ptr1 << endl;
    cout << " ptr2: " << ptr2 << endl;
    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: auto_ptr ownership issues

#2 Karel-Lodewijk   User is offline

  • D.I.C Addict
  • member icon

Reputation: 454
  • View blog
  • Posts: 864
  • Joined: 17-March 11

Re: auto_ptr ownership issues

Posted 11 February 2012 - 03:01 AM

In c++11 auto_ptr has been deprecated, use unique_ptr instead.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1