fork(1) download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. auto_ptr<int> p(new int(42));
  9. auto_ptr<int> q;
  10.  
  11. cout << "after initialization:" << endl;
  12. cout << " p: " << (p.get() ? *p : 0) << endl;
  13. cout << " q: " << (q.get() ? *q : 0) << endl;
  14.  
  15. q = p;
  16. cout << "after assigning auto pointers:" << endl;
  17. cout << " p: " << (p.get() ? *p : 0) << endl;
  18. cout << " q: " << (q.get() ? *q : 0) << endl;
  19.  
  20. *q += 13; // change value of the object q owns
  21. p = q;
  22. cout << "after change and reassignment:" << endl;
  23. cout << " p: " << (p.get() ? *p : 0) << endl;
  24. cout << " q: " << (q.get() ? *q : 0) << endl;
  25. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
after initialization:
 p: 42
 q: 0
after assigning auto pointers:
 p: 0
 q: 42
after change and reassignment:
 p: 55
 q: 0