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. shared_ptr<Obj> foo() {
  25. out("in foo");
  26. shared_ptr<Obj> tmp(new Obj);
  27. out("out foo");
  28. return tmp;
  29. }
  30.  
  31. int main() {
  32. out("start");
  33. shared_ptr<Obj> o(new Obj);
  34. shared_ptr<Obj> e, p;
  35. out("call foo");
  36. o = foo();
  37. e = o;
  38. p = move(o);
  39. out("back to main");
  40. out(o.get());
  41. out(e.get());
  42. out(p.get());
  43. out("finish");
  44. return 0;
  45. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
start
new 0
call foo
in foo
new 1
out foo
delete 0
back to main
0
0x9056030
0x9056030
finish
delete 1