Advertisement
junghu1124

Untitled

Sep 7th, 2023 (edited)
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | Source Code | 0 0
  1. #include <iostream>
  2.  
  3. class T {
  4.     int i = 0;
  5.  
  6. public:
  7.     explicit T(int i): i(i) {}; // 기본 생성자
  8.  
  9.     T operator+ (const T& t) const {
  10.         T t_(i + t.i);
  11.         return t_;
  12.     }
  13.  
  14.     T(T& t) { // 복사 생성자
  15.         std::cout << "copy called" << std::endl;
  16.         i = t.i;
  17.         std::cout << "constructor ended" << std::endl;
  18.     }
  19.  
  20.     T(T&& t) noexcept { // 이동 생성자
  21.         std::cout << "move called" << std::endl;
  22.         i = t.i;
  23.         std::cout << "constructor ended" << std::endl;
  24.     }
  25.  
  26.     ~T() { // 소멸자
  27.         std::cout << "destroyed" << std::endl;
  28.     }
  29. };
  30.  
  31. int main() {
  32.     T t1(10);
  33.     T t2(20);
  34.  
  35.     std::cout << "(1) ============" << std::endl;
  36.     T&& t3 = t1 + t2;
  37.     std::cout << std::endl;
  38.  
  39.     std::cout << "(2) ============" << std::endl;
  40.     T t3_1 = t1 + t2;
  41.     std::cout << std::endl;
  42.  
  43.     std::cout << "(3) ============" << std::endl;
  44.     const T& t4 = t1 + t2;
  45.     std::cout << std::endl;
  46.  
  47.     std::cout << "(4) ============" << std::endl;
  48.     T& t5 = t1;
  49.     std::cout << std::endl;
  50.  
  51.     std::cout << "(5) ============" << std::endl;
  52.     T t5_1 = t1;
  53.     std::cout << std::endl;
  54.  
  55.     std::cout << "code fin\n" << std::endl;
  56.  
  57.     return 0;
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement