stdmove.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // stdmove.cpp -- using std::move()
  2. #include <iostream>
  3. #include <utility>
  4. // use the following for g++4.5
  5. // #define nullptr 0
  6. // interface
  7. class Useless
  8. {
  9. private:
  10. int n; // number of elements
  11. char * pc; // pointer to data
  12. static int ct; // number of objects
  13. void ShowObject() const;
  14. public:
  15. Useless();
  16. explicit Useless(int k);
  17. Useless(int k, char ch);
  18. Useless(const Useless & f); // regular copy constructor
  19. Useless(Useless && f); // move constructor
  20. ~Useless();
  21. Useless operator+(const Useless & f)const;
  22. Useless & operator=(const Useless & f); // copy assignment
  23. Useless & operator=(Useless && f); // move assignment
  24. void ShowData() const;
  25. };
  26. // implementation
  27. int Useless::ct = 0;
  28. Useless::Useless()
  29. {
  30. ++ct;
  31. n = 0;
  32. pc = nullptr;
  33. }
  34. Useless::Useless(int k) : n(k)
  35. {
  36. ++ct;
  37. pc = new char[n];
  38. }
  39. Useless::Useless(int k, char ch) : n(k)
  40. {
  41. ++ct;
  42. pc = new char[n];
  43. for (int i = 0; i < n; i++)
  44. pc[i] = ch;
  45. }
  46. Useless::Useless(const Useless & f): n(f.n)
  47. {
  48. ++ct;
  49. pc = new char[n];
  50. for (int i = 0; i < n; i++)
  51. pc[i] = f.pc[i];
  52. }
  53. Useless::Useless(Useless && f): n(f.n)
  54. {
  55. ++ct;
  56. pc = f.pc; // steal address
  57. f.pc = nullptr; // give old object nothing in return
  58. f.n = 0;
  59. }
  60. Useless::~Useless()
  61. {
  62. delete [] pc;
  63. }
  64. Useless & Useless::operator=(const Useless & f) // copy assignment
  65. {
  66. std::cout << "copy assignment operator called:\n";
  67. if (this == &f)
  68. return *this;
  69. delete [] pc;
  70. n = f.n;
  71. pc = new char[n];
  72. for (int i = 0; i < n; i++)
  73. pc[i] = f.pc[i];
  74. return *this;
  75. }
  76. Useless & Useless::operator=(Useless && f) // move assignment
  77. {
  78. std::cout << "move assignment operator called:\n";
  79. if (this == &f)
  80. return *this;
  81. delete [] pc;
  82. n = f.n;
  83. pc = f.pc;
  84. f.n = 0;
  85. f.pc = nullptr;
  86. return *this;
  87. }
  88. Useless Useless::operator+(const Useless & f)const
  89. {
  90. Useless temp = Useless(n + f.n);
  91. for (int i = 0; i < n; i++)
  92. temp.pc[i] = pc[i];
  93. for (int i = n; i < temp.n; i++)
  94. temp.pc[i] = f.pc[i - n];
  95. return temp;
  96. }
  97. void Useless::ShowObject() const
  98. {
  99. std::cout << "Number of elements: " << n;
  100. std::cout << " Data address: " << (void *) pc << std::endl;
  101. }
  102. void Useless::ShowData() const
  103. {
  104. if (n == 0)
  105. std::cout << "(object empty)";
  106. else
  107. for (int i = 0; i < n; i++)
  108. std::cout << pc[i];
  109. std::cout << std::endl;
  110. }
  111. // application
  112. int main()
  113. {
  114. using std::cout;
  115. {
  116. Useless one(10, 'x');
  117. Useless two = one +one; // calls move constructor
  118. cout << "object one: ";
  119. one.ShowData();
  120. cout << "object two: ";
  121. two.ShowData();
  122. Useless three, four;
  123. cout << "three = one\n";
  124. three = one; // automatic copy assignment
  125. cout << "now object three = ";
  126. three.ShowData();
  127. cout << "and object one = ";
  128. one.ShowData();
  129. cout << "four = one + two\n";
  130. four = one + two; // automatic move assignment
  131. cout << "now object four = ";
  132. four.ShowData();
  133. cout << "four = move(one)\n";
  134. four = std::move(one); // forced move assignment
  135. cout << "now object four = ";
  136. four.ShowData();
  137. cout << "and object one = ";
  138. one.ShowData();
  139. }
  140. std::cin.get();
  141. }