Advertisement
D3ad

Untitled

Dec 23rd, 2020
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <memory>
  3. #include <cassert>
  4.  
  5. class foo {
  6. public:
  7.     void print() {
  8.         std::cout << "foo::print" << std::endl;
  9.     }  
  10. };
  11.  
  12. class bar {
  13. public:
  14.     std::unique_ptr<foo> ptr;
  15. };
  16.  
  17. int main()
  18. {
  19.     std::unique_ptr<foo> a = std::make_unique<foo>();
  20.     a->print();
  21.  
  22.     assert(a);
  23.     bar new_owner;
  24.     new_owner.ptr = std::move(a);
  25.     assert(!a);
  26.     new_owner.ptr->print();
  27.  
  28.  
  29.     return 0;
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement