daily pastebin goal
16%
SHARE
TWEET

Untitled

a guest Sep 14th, 2018 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. c  11 optimal parameter passing
  2. class A
  3. {
  4.     string test;
  5. public:
  6.     A (string _t) : test(move(_t)) {}
  7.     A (const A & other) { *this = other; }
  8.     A (A && other) { *this = move(other); }
  9.  
  10.     A & operator = (const A & other)
  11.     {
  12.         cerr<<"copying A"<<endl;
  13.         test = other.test;
  14.         return *this;
  15.     }
  16.  
  17.     A & operator = (A && other)
  18.     {
  19.         cerr<<"move A"<<endl;
  20.         test = other.test;
  21.         return *this;
  22.     }
  23. };
  24.  
  25. class B
  26. {
  27.     A a;
  28. public:  
  29.     B (A && _a) : a(move(_a)) {}
  30.     B (A const & _a) : a(_a) {}
  31. };
  32.    
  33. class B
  34. {
  35.     A a;
  36. public:  
  37.     B (A _a) : a(move(_a)) {}
  38. };
  39.    
  40. class B
  41. {
  42.     A a;
  43. public:  
  44.     template <class T,
  45.               class = typename std::enable_if
  46.               <
  47.                  std::is_constructible<A, T>::value
  48.               >::type>
  49.     B (T&& _a) : a(std::forward<T>(_a)) {}
  50. };
  51.    
  52. template <typename T> explicit B(T && x) : a(std::forward<T>(x) { }
  53.    
  54. A::A(string s) :test(std::move(s)) {}
  55.    
  56. class A
  57. {
  58. public:
  59.     A() :p() {}
  60.  
  61.     A(const A& a) :p(new data(*a.p)) {} //copy
  62.     A(A&& a) :p(a.p) { a.p=0; }         //move
  63.  
  64.     A& operator=(A a) //note: pass by value
  65.     { clear(); swap(a); return *this; }
  66.     ~A() { clear(); }
  67.  
  68.     void swap(A& a) { std::swap(p,a.p); }
  69.     void clear() { delete p; p=0; }
  70.  
  71. private:
  72.  
  73.     data* p;
  74. };
  75.    
  76. class B: public A
  77. {
  78. ...
  79. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top