1. 程式人生 > 實用技巧 >Designs and Declarations

Designs and Declarations

  1 //provision_21: Don't try to return a reference when you must return an object
  2 //static :不能比較
  3 #include<iostream>
  4 class Rational
  5 {
  6     friend const Rational&
  7         operator*(const Rational& lhs, const Rational& rhs);
  8     friend bool
  9         operator
==(const Rational& lhs, const Rational& rhs); 10 public: 11 Rational(int _n = 0, int _d = 1) :n(_n), d(_d) {} 12 private: 13 int n,d; 14 }; 15 16 const Rational& 17 operator*(const Rational& lhs, const Rational& rhs) 18 { 19 static Rational result; //只在第一個作用 20
result.n = lhs.n * rhs.n; 21 result.d = lhs.d * rhs.d; 22 return result; 23 } 24 bool operator==(const Rational& lhs, const Rational& rhs) 25 { 26 return ((lhs.n == rhs.n) && (lhs.d == rhs.d)) ? true : false; 27 } 28 int main() 29 { 30 Rational a, b(1,2
), c(3,4), d(5,6); 31 if ((a * b) == (c * d)) //返回的都是 staic result 的引用: if( static Rational& == static Rational&) 32 //換句話說:在operator==(l,r) 時比較的都是最後一次operator*(a,b)的result 而(c*d)存放的值被(a*b)替換 33 std::cout << "always true" << std::endl; 34 return 0; 35 } 36 37 //Provision_24 Declare non-member functions when type conversions should apply to all parameters 38 //當不涉及template 時,如果可以避免friend 函式就該避免 39 class Ration 40 { 41 public: 42 Ration(int _n = 0, int _d = 1) :n(_n), d(_d) {} 43 int nf() const { return n; } //const 指標 只能繫結 const this* 44 int df() const { return d; } 45 private: 46 int n; 47 int d; 48 }; 49 inline const Ration 50 operator*(const Ration& lhs, const Ration& rhs) 51 { 52 return Ration(lhs.nf() * rhs.nf(), lhs.df() * rhs.df()); 53 } 54 #include<vector> 55 //Provision_25 Consider support for a non-throwing swap 56 class WidgetImpl //針對Widget資料設計的class 57 { 58 public: 59 60 private: 61 std::vector<double> vd; 62 }; 63 64 class Widget 65 { 66 public: 67 Widget& operator=(const Widget& rhs) 68 { 69 //...其他資料 70 *pImpl = *(rhs.pImpl); 71 } 72 73 void swap(Widget& other) //因為使用private member 所以是成員函式 74 { 75 using std::swap; //若沒有自定義swap 使用 std::swap 76 swap(pImpl, other.pImpl); 77 } 78 private: 79 WidgetImpl* pImpl; 80 }; 81 namespace std 82 { 83 template<> //只是特例化 Widget 84 void swap<Widget>(Widget& a, Widget& b) 85 { 86 a.swap(b);//呼叫 swap成員函式 87 } 88 } 89 90 //如果Widget<T> 91 namespace WidgetStuff //不能改變std::swap 92 { 93 template<typename T> 94 class WidgetImpl 95 { 96 private: 97 std::vector<T> v; 98 }; 99 100 template<typename T> 101 class Widget 102 { 103 public: 104 void swap(Widget& other) //因為使用private member 所以是成員函式 105 { 106 using std::swap; //若沒有自定義swap 使用 std::swap 107 swap(pImpl, other.pImpl); 108 } 109 private: 110 WidgetImpl<T>* pImpl; 111 }; 112 113 template<typename T> 114 void swap(Widget<T>& a, Widget<T>& b) 115 { 116 a.swap(b); 117 } 118 }