Facebook
From Rude Owl, 5 Months ago, written in C++.
Embed
Download Paste or View Raw
Hits: 124
  1. #include <iostream>
  2. #include <string>
  3.  
  4. class Test {
  5. public:
  6.     static unsigned int _id;
  7.  
  8.     Test(): _a(1000) {
  9.         _effective_id = _own_id = _id++;
  10.         log("Default constructor");
  11.     };
  12.     Test(int a): _a(a) {
  13.         _effective_id = _own_id = _id++;
  14.         log("Int constructor with value " + std::to_string(a));
  15.     };
  16.  
  17.     Test(const Test& other) {
  18.         _a = other._a;
  19.         _effective_id = _own_id = _id++;
  20.         log("Copy constructor with id " + std::to_string(other._own_id));
  21.     }
  22.  
  23.     Test(Test&& other) {
  24.         _a = other._a;
  25.         _effective_id = _id++;
  26.         _own_id = other._own_id;
  27.         log("Move constructor with id " + std::to_string(other._own_id));
  28.         other._move_made = true;
  29.     }
  30.  
  31.     ~Test() {
  32.         log("Destructor");
  33.         if(_move_made) {
  34.             log("After move");
  35.         }
  36.     }
  37.  
  38.     Test& operator=(const Test& other) {
  39.         _a = other._a;
  40.         log("Copy operator with id " + std::to_string(other._own_id));
  41.         return *this;
  42.     }  
  43.  
  44.     Test& operator=(Test&& other) {
  45.         _a = other._a;
  46.         other._move_made = true;
  47.         _effective_id = _own_id;
  48.         _own_id = other._own_id;
  49.         log("Move copy operator with id " + std::to_string(other._own_id));
  50.         return *this;
  51.     }
  52.  
  53. private:
  54.     int _a;
  55.     unsigned int _own_id, _effective_id;
  56.     bool _move_made = false;
  57.    
  58.     void log(std::string msg) {
  59.         std::cout << _own_id << "(effective: " << _effective_id << ")" << ": " + msg + "n";
  60.     }
  61. };
  62.  
  63. Test get_test() {
  64.     std::cout << "Inside get_test()n";
  65.     return Test(5);
  66. }
  67.  
  68. Test&& get_move() {
  69.     std:: cout << "Inside get_move()n";
  70.     return std::move(Test(7));
  71. }
  72.  
  73. void copy_pass(Test copy) {
  74.     std::cout << "Took by copyn";
  75. }
  76.  
  77. void pass(const Test& ref) {
  78.     std::cout << "Took by refn";
  79. }
  80.  
  81. void pass(Test&& subject) {
  82.      std::cout << "Took by rvaln";
  83. }
  84.  
  85. unsigned int Test::_id = 0;
  86.  
  87. int main() {
  88.     std::cout << "Initialize an";
  89.     Test a = get_test();
  90.     std::cout << "Pass by copyn";
  91.     copy_pass(a);
  92.     std::cout << "Pass by refn";
  93.     pass(a);
  94.     std::cout << "Pass by moven";
  95.     pass(std::move(a));
  96.  
  97.     std::cout << "Initializing bn";
  98.     Test b(7);
  99.     std::cout << "Move to cn";
  100.     auto c = std::move(b);
  101.     std::cout << "Ref to dn";
  102.     auto& d = c;
  103.     std::cout << "Copy by ref to en";
  104.     Test e(d);
  105.     std::cout << "Copy by rval to fn";
  106.     Test f(std::move(e));
  107.  
  108.     std::cout << "Getting by moven";
  109.     Test g(get_move());
  110. }