fork download
  1. #include <iostream>
  2.  
  3. class Whatever
  4. {
  5. private:
  6. int mSomeshit;
  7. public:
  8. Whatever() : mSomeshit(0) { }
  9.  
  10. Whatever(int someshit) : mSomeshit(someshit)
  11. {
  12. std::cout << "retain " << mSomeshit << std::endl;
  13. }
  14.  
  15. Whatever(Whatever& that) : mSomeshit(that.mSomeshit)
  16. {
  17. std::cout << "Copy c-tor" << std::endl;
  18. retain();
  19. }
  20.  
  21. Whatever(Whatever&& that) : Whatever()
  22. {
  23. std::cout << "Move c-tor" << std::endl;
  24. std::swap(mSomeshit, that.mSomeshit);
  25. }
  26.  
  27. Whatever& operator=(Whatever that)
  28. {
  29. std::cout << "Assignment operator" << std::endl;
  30. std::swap(mSomeshit, that.mSomeshit);
  31. return *this;
  32. }
  33.  
  34. ~Whatever()
  35. {
  36. if (0 != mSomeshit)
  37. std::cout << "release " << mSomeshit << std::endl;
  38. }
  39.  
  40. void retain()
  41. {
  42. std::cout << "retain " << mSomeshit << std::endl;
  43. }
  44. };
  45.  
  46. class Context
  47. {
  48. public:
  49. Whatever createWhatever(int someshit)
  50. {
  51. return Whatever(someshit);
  52. }
  53. };
  54.  
  55. int main() {
  56. Context context;
  57.  
  58. auto whateverInstance1 = std::move(context.createWhatever(2));
  59. auto whateverInstance2 = std::move(context.createWhatever(5));
  60. auto whateverInstance3 = std::move(context.createWhatever(9));
  61.  
  62. whateverInstance1 = whateverInstance2;
  63.  
  64. whateverInstance2 = std::move(whateverInstance3);
  65.  
  66. return 0;
  67. }
Success #stdin #stdout 0s 3100KB
stdin
Standard input is empty
stdout
retain 2
Move c-tor
retain 5
Move c-tor
retain 9
Move c-tor
Copy c-tor
retain 5
Assignment operator
release 2
Move c-tor
Assignment operator
release 5
release 9
release 5