Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <utility>
  3. #include <memory>
  4.  
  5. unsigned int counter = 1;
  6.  
  7. class Stuff {
  8.     private:
  9.         unsigned int y;
  10.  
  11.     public:
  12.         Stuff(unsigned int x) {
  13.             y = x * x;
  14.         }
  15. };
  16.  
  17. class A {
  18.     public:
  19.         int id;
  20.  
  21.         A() {
  22.             id = counter++;
  23.  
  24.             ptr = std::make_unique<Stuff>(id);
  25.         };
  26.  
  27.         std::unique_ptr<Stuff>& get_data() {
  28.             return ptr;
  29.         }
  30.  
  31.         A(A&& to_move) {
  32.             id = to_move.id;
  33.  
  34.             ptr = std::move(to_move.get_data());
  35.         }
  36.  
  37.     private:
  38.         std::unique_ptr<Stuff> ptr;
  39. };
  40.  
  41. A get_A() {
  42.     return A();
  43. }
  44.  
  45. int main() {
  46.     A a;
  47.     A b = std::move(a);
  48.     A c = get_A();
  49.     A d = std::move(get_A());
  50.  
  51.     std::cout << a.id << b.id << c.id << d.id << '\n';
  52.  
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement