Advertisement
35657

Untitled

Mar 2nd, 2024
685
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1.  
  2. #define _CRT_SECURE_NO_WARNINGS
  3.  
  4. #include <iostream>
  5. #include <cstring>
  6.  
  7. using namespace std;
  8.  
  9. class Test {
  10.  
  11. public:
  12.  
  13.     Test(const int& value) : value_(value) {
  14.         cout << "parameterized constructor for " << this << endl;
  15.     }
  16.  
  17.     Test(const Test& other) : value_(other.value_) {
  18.         cout << "copy constructor for " << this << endl;
  19.     }
  20.  
  21.     Test& operator=(const Test& other) {
  22.         if (this != &other) {
  23.             value_ = other.value_;
  24.         }
  25.         cout << "operator = for Test for " << this << endl;
  26.         return *this;
  27.     }
  28.  
  29.     Test& operator=(const int& value) {
  30.         value_ = value;
  31.         cout << "operator = for " << this << endl;
  32.         return *this;
  33.     }
  34.  
  35.  
  36.     friend ostream& operator<<(ostream& out, const Test& test);
  37.  
  38. private:
  39.  
  40.     int value_ = 1;
  41.  
  42. };
  43.  
  44. ostream& operator<<(ostream& out, const Test& test) {
  45.     out << test.value_;
  46.     return out;
  47. }
  48.  
  49. int func(int x) {
  50.     return x * 2;
  51. }
  52.  
  53. int main() {
  54.  
  55.     Test a(3); // слева именованный(lvalue) объект, справа временный объект(rvalue) типа int (целочисленный литерал)
  56.  
  57.     Test b(2); // слева именованный(lvalue) объект, справа временный объект(rvalue) типа int
  58.  
  59.     a = 3; // слева именованный (lvalue) объект, справа временный объект (rvalue) типа int
  60.  
  61.     a = 3 + 4; // слева именованный (lvalue) объект, справа временный объект (rvalue) - результат выражения
  62.  
  63.     a = func(5); // слева именованный (lvalue) объект, справа временный объект (rvalue) - результат работы функции
  64.  
  65.     a = b; // слева и справа именованные (lvalue) объекты
  66.  
  67.     cout << a << endl;
  68.  
  69.     cout << b << endl;
  70.  
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement