uselessm.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // uselessm.cpp -- an otherwise useless class with move semantics
  2. #include <iostream>
  3. #include <utility>
  4. using namespace std;
  5. // interface
  6. class Useless
  7. {
  8. private:
  9. int n; // number of elements
  10. char * pc; // pointer to data
  11. static int ct; // number of objects
  12. void ShowObject() const;
  13. public:
  14. Useless();
  15. explicit Useless(int k);
  16. Useless(int k, char ch);
  17. Useless(const Useless & f); // regular copy constructor
  18. Useless(Useless && f); // move constructor
  19. ~Useless();
  20. Useless operator+(const Useless & f)const;
  21. // need operator=() in copy and move versions
  22. void ShowData() const;
  23. };
  24. // implementation
  25. int Useless::ct = 0;
  26. Useless::Useless()
  27. {
  28. ++ct;
  29. n = 0;
  30. pc = nullptr;
  31. cout << "default constructor called; number of objects: " << ct << endl;
  32. ShowObject();
  33. }
  34. Useless::Useless(int k) : n(k)
  35. {
  36. ++ct;
  37. cout << "int constructor called; number of objects: " << ct << endl;
  38. pc = new char[n];
  39. ShowObject();
  40. }
  41. Useless::Useless(int k, char ch) : n(k)
  42. {
  43. ++ct;
  44. cout << "int, char constructor called; number of objects: " << ct << endl;
  45. pc = new char[n];
  46. for (int i = 0; i < n; i++)
  47. pc[i] = ch;
  48. ShowObject();
  49. }
  50. Useless::Useless(const Useless & f): n(f.n)
  51. {
  52. ++ct;
  53. cout << "copy const called; number of objects: " << ct << endl;
  54. pc = new char[n];
  55. for (int i = 0; i < n; i++)
  56. pc[i] = f.pc[i];
  57. ShowObject();
  58. }
  59. Useless::Useless(Useless && f): n(f.n)
  60. {
  61. ++ct;
  62. cout << "move constructor called; number of objects: " << ct << endl;
  63. pc = f.pc; // steal address
  64. f.pc = nullptr; // give old object nothing in return
  65. f.n = 0;
  66. ShowObject();
  67. }
  68. Useless::~Useless()
  69. {
  70. cout << "destructor called; objects left: " << --ct << endl;
  71. cout << "deleted object:\n";
  72. ShowObject();
  73. delete [] pc;
  74. }
  75. Useless Useless::operator+(const Useless & f)const
  76. {
  77. cout << "Entering operator+()\n";
  78. Useless temp = Useless(n + f.n);
  79. for (int i = 0; i < n; i++)
  80. temp.pc[i] = pc[i];
  81. for (int i = n; i < temp.n; i++)
  82. temp.pc[i] = f.pc[i - n];
  83. cout << "temp object:\n";
  84. cout << "Leaving operator+()\n";
  85. return temp;
  86. }
  87. void Useless::ShowObject() const
  88. {
  89. cout << "Number of elements: " << n;
  90. cout << " Data address: " << (void *) pc << endl;
  91. }
  92. void Useless::ShowData() const
  93. {
  94. for (int i = 0; i < n; i++)
  95. cout << pc[i];
  96. cout << endl;
  97. }
  98. // application
  99. int main()
  100. {
  101. {
  102. Useless one(10, 'x');
  103. Useless two = one; // calls copy constructor
  104. cout << "object one: ";
  105. one.ShowData();
  106. cout << "object two: ";
  107. two.ShowData();
  108. Useless three = move(one);
  109. cout << "object three: ";
  110. three.ShowData();
  111. }
  112. cin.get();
  113. }