fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <utility>
  4.  
  5. struct Foo
  6. {
  7. Foo()
  8. {
  9. m_Bar = new int;
  10. *m_Bar = s_Baz++;
  11. printf("Ctor %8p %i\n", m_Bar, *m_Bar);
  12. }
  13.  
  14. Foo(const Foo& other)
  15. {
  16. m_Bar = new int;
  17. *m_Bar = *(other.m_Bar);
  18. printf("Copy %8p %i\n", m_Bar, *m_Bar);
  19. }
  20.  
  21. Foo(Foo&& other)
  22. {
  23. m_Bar = other.m_Bar;
  24. other.m_Bar = nullptr;
  25. printf("Move %8p %i\n", m_Bar, *m_Bar);
  26. }
  27.  
  28. ~Foo()
  29. {
  30. printf("Dtor %8p\n", m_Bar);
  31. delete m_Bar;
  32. }
  33.  
  34. int* m_Bar;
  35. static int s_Baz;
  36. };
  37.  
  38. int Foo::s_Baz;
  39.  
  40. void DoFoo(Foo foo)
  41. {
  42. printf("Foo %8p %i\n", foo.m_Bar, *(foo.m_Bar));
  43. }
  44.  
  45. int main()
  46. {
  47. Foo foo;
  48. DoFoo(foo);
  49. DoFoo(Foo{});
  50. return 0;
  51. }
  52.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
Ctor 0x96eaa10 0
Copy 0x96eaa20 0
Foo  0x96eaa20 0
Dtor 0x96eaa20
Ctor 0x96eaa20 1
Foo  0x96eaa20 1
Dtor 0x96eaa20
Dtor 0x96eaa10