fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class C
  6. {
  7. public:
  8. C(std::string someName) : name(someName) {}
  9.  
  10. void swap(C& other)
  11. {
  12. name.swap(other.name);
  13. cout << "swap, other = " << other.name << "(" << &other << ")" << endl;
  14. }
  15.  
  16. C(C&& other) // конструктор копирования
  17. {
  18. this->swap(other);
  19. }
  20.  
  21. C& operator=(C other) // оператор присваивания
  22. { // передача параметра по значению важна!
  23. swap(other); // обмен с временной копией
  24. return *this;
  25. }
  26.  
  27. std::string name;
  28. };
  29.  
  30. int main(int argc, char* argv[])
  31. {
  32. C c1("c1");
  33. C c2(c1);
  34. cout << "c1 -> " << c1.name << endl;
  35. cout << "c2 -> " << c2.name << endl;
  36. }
Success #stdin #stdout 0s 3060KB
stdin
Standard input is empty
stdout
c1 -> c1
c2 -> c1