Advertisement
giGii

test_copy_elision

Dec 4th, 2022
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. static size_t counter = 0;
  5.  
  6. using namespace std;
  7.  
  8. class Number {
  9. private:
  10.     int a_;
  11.  
  12. public:
  13.     Number() {
  14.         cout << "Default Ctor"s << endl;
  15.         cout << "at " << this << endl;
  16.         ++counter;
  17.     }
  18.     Number(int num)
  19.         : a_(num)
  20.     {
  21.         cout << "Ctor(int)"s << endl;
  22.         cout << "at " << this << endl;
  23.         ++counter;
  24.  
  25.     }
  26.     Number(const Number& other)
  27.         : a_(other.a_)
  28.     {
  29.         cout << "Copy Ctor"s << endl;
  30.         cout << "at " << this << endl;
  31.         cout << "other " << this << endl;
  32.         ++counter;
  33.  
  34.     }
  35.     ~Number() {
  36.         cout << "Dtor"s << endl;
  37.         cout << "at " << this << endl;
  38.         ++counter;
  39.  
  40.     }
  41.     Number& operator=(const Number& other) {
  42.         a_ = other.a_;
  43.         cout << "Op="s << endl;
  44.         cout << "this op= " << this << endl;
  45.         cout << "other " << &other << endl;
  46.         ++counter;
  47.  
  48.         return *this;
  49.     }
  50.     Number operator+(const Number& second) {
  51.         cout << "Op+" << endl;
  52.         cout << "this op+ " << this << endl;
  53.         cout << "second " << &second << endl;
  54.         Number tmp_num(a_ + second.a_);
  55.         cout << "tmp_num " << &tmp_num << endl;
  56.         ++counter;
  57.  
  58.         return a_ + second.a_;
  59.     }
  60. };
  61.  
  62. int main() {
  63.     Number a(1), b(1), c;
  64.     c = a + b;
  65.     cout << "End of calculations"s << endl;
  66.     cout << counter << endl;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement