fork(1) download
  1. #include <iostream>
  2. #include <memory>
  3. using namespace std;
  4.  
  5. template <class T>
  6. inline void out(const T& t) { cout << t << endl; }
  7. inline void out(const char* str) { cout << str << endl; }
  8. template <class T>
  9. inline void out(const char* str, const T& t)
  10. { cout << str << ' ' << t << endl; }
  11.  
  12. class Obj
  13. {
  14. private:
  15. static int count;
  16. int num;
  17. public:
  18. Obj() : num(count++) { out("new", num); }
  19. ~Obj() { out("delete", num); }
  20. void o() { out("mynum", num); }
  21. };
  22. int Obj::count = 0;
  23.  
  24. auto_ptr<Obj> foo() {
  25. out("in foo");
  26. auto_ptr<Obj> tmp(new Obj);
  27. out("out foo");
  28. return tmp;
  29. }
  30.  
  31. int main() {
  32. out("start");
  33. auto_ptr<Obj> o(new Obj);
  34. auto_ptr<Obj> e(new Obj);
  35. auto_ptr<Obj> p(new Obj);
  36. out("call foo");
  37. o = foo();
  38. e = o;
  39. p = move(o);
  40. out("back to main");
  41. out(o.get());
  42. out(e.get());
  43. out(p.get());
  44. out("finish");
  45. return 0;
  46. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
start
new 0
new 1
new 2
call foo
in foo
new 3
out foo
delete 0
delete 1
delete 2
back to main
0
0x93a7038
0
finish
delete 3