onilink_

move & swap

Aug 27th, 2019
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.46 KB | None | 0 0
  1.  
  2. struct A { int a=0, b=0, c=0; };
  3. struct B: A {
  4.     B() = default;
  5.     B(const B& o):
  6.         A(o), d(o.d?std::make_unique<int>(*o.d):nullptr){}
  7.     B(B&& o) { swap(*this, o); }
  8.     ~B() = default;
  9.    
  10.     B& operator=(B o) { swap(*this, o); return *this; }
  11.    
  12.     friend void swap(B&,B&);
  13.    
  14.     std::unique_ptr<int> d;
  15. };
  16.  
  17. void swap(B& a, B& b) {
  18.     using std::swap;
  19.     swap(static_cast<A&>(a), static_cast<A&>(b));
  20.     swap(a.d, b.d);
  21. }
Advertisement
Add Comment
Please, Sign In to add comment