fork download
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. class A
  5. {
  6. public:
  7. A()
  8. {
  9. myCtr = ++ctr;
  10. printf("class A default Constructor - object id: %u\n", myCtr);
  11. }
  12.  
  13. A(const A &a2) {
  14. myCtr = ++ctr;
  15. printf("class A copy constructor - object id: %u\n", myCtr);
  16.  
  17. }
  18.  
  19. A(A &&a2) {
  20. myCtr = a2.myCtr;
  21. a2.myCtr = 0;
  22.  
  23. printf("class A move constructor - object id: %u\n", myCtr);
  24.  
  25. }
  26.  
  27. A & operator=(const A &a2) {
  28. myCtr = ++ctr;
  29.  
  30. printf("class A copy assignment - from object id: %u - to object id: %u\n", a2.myCtr, myCtr);
  31.  
  32. return *this;
  33. }
  34.  
  35. A & operator=(A &&a2) {
  36. printf("class A move assignment - from object id: %u - to object id: %u\n", a2.myCtr, myCtr);
  37.  
  38. if (this != &a2) {
  39. //myCtr = a2.myCtr;
  40. //a2.myCtr = 0;
  41. }
  42.  
  43. return *this;
  44. }
  45.  
  46. ~A()
  47. {
  48. printf("class A destructor - object id: %u\n", myCtr);
  49. }
  50.  
  51. private:
  52. uint64_t myCtr;
  53. static uint64_t ctr;
  54. };
  55.  
  56. class B
  57. {
  58. public:
  59. B() {
  60.  
  61. }
  62.  
  63. B(char * input, uint32_t len) {
  64. for (uint32_t i = 0; i < len; i++)
  65. {
  66. /* do something */
  67. }
  68. }
  69.  
  70. B(const B &b2) : characters(b2.characters)
  71. {
  72. }
  73.  
  74. B(B &&b2) {
  75. characters = A(b2.characters);
  76. }
  77.  
  78. B & operator=(const B &b2) {
  79. characters = A(b2.characters);
  80. }
  81.  
  82. B & operator=(B &&b2) {
  83. characters = A(b2.characters);
  84. }
  85.  
  86. ~B() {
  87.  
  88. }
  89.  
  90.  
  91. private:
  92. A characters;
  93. };
  94.  
  95. uint64_t A::ctr = 0;
  96.  
  97. int main(int argc, char *argv[]) {
  98. B b1 = B((char *)"b1", 2);
  99. B b2 = b1;
  100.  
  101. return 0;
  102. }
Success #stdin #stdout 0s 4372KB
stdin
Standard input is empty
stdout
class A default Constructor - object id: 1
class A copy constructor - object id: 2
class A destructor - object id: 2
class A destructor - object id: 1