fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class test {
  5. public:
  6. test(int input = 0)
  7. : value(input)
  8. {
  9. std::cout << "default constructor called with " << value << "\n";
  10. }
  11.  
  12. test(test const& other)
  13. : value(other.value)
  14. {
  15. std::cout << "copy constructor called with " << value << "\n";
  16. }
  17.  
  18. test(test&& other) noexcept
  19. : value(0)
  20. {
  21. std::swap(value, other.value);
  22. std::cout << "move constructor called with " << value << "\n";
  23. }
  24.  
  25. test& operator=(test const& other)
  26. {
  27. value = other.value;
  28. std::cout << "copy assignment called with " << value << "\n";
  29. return *this;
  30. }
  31.  
  32. test& operator=(test&& other) noexcept
  33. {
  34. std::swap(value, other.value);
  35. std::cout << "move assignment called with " << value << "\n";
  36. return *this;
  37. }
  38. private:
  39. int value;
  40. };
  41.  
  42. int main() {
  43. test myTest1 = std::move(test(1));
  44. test myTest2 = test(2);
  45. myTest1 = myTest2;
  46. myTest1 = std::move(myTest2);
  47. // your code goes here
  48. return 0;
  49. }
Success #stdin #stdout 0s 4360KB
stdin
Standard input is empty
stdout
default constructor called with 1
move constructor called with 1
default constructor called with 2
copy assignment called with 2
move assignment called with 2