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 }
原文地址:https://www.cnblogs.com/Z-s-c11/p/14158187.html