fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. class Movable {
  8. public:
  9. Movable(const string& name) : m_name(name) { }
  10. Movable(const Movable& rhs) {
  11. cout << "Copy constructed from " << rhs.m_name << endl;
  12. }
  13.  
  14. Movable(Movable&& rhs) {
  15. cout << "Move constructed from " << rhs.m_name << endl;
  16. }
  17.  
  18. Movable& operator = (const Movable& rhs) {
  19. cout << "Copy assigned from " << rhs.m_name << endl;
  20. }
  21.  
  22. Movable& operator = (Movable&& rhs) {
  23. cout << "Move assigned from " << rhs.m_name << endl;
  24. }
  25.  
  26. private:
  27. string m_name;
  28. };
  29.  
  30.  
  31. int main() {
  32. Movable obj1("obj1");
  33. Movable obj2(std::move(obj1));
  34. obj2 = std::move(obj1); // For demostration only
  35.  
  36. const Movable cObj("cObj");
  37. Movable tObj(std::move(cObj));
  38. tObj = std::move(cObj); // For demonstration only
  39. }
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
Move constructed from obj1
Move assigned from obj1
Copy constructed from cObj
Copy assigned from cObj