useless.cpp 2.9 KB

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