设为首页 加入收藏

TOP

8.4.2 实现对比较运算符的完全支持(1)
2013-10-07 16:07:56 来源: 作者: 【 】 浏览:56
Tags:8.4.2 实现 比较 运算 符的 完全 支持

8.4.2  实现对比较运算符的完全支持(1)

有了前面实现的operator<()运算符函数,我们仍然有许多事情不能做。用CBox对象指定问题的解决方案可能涉及像下面这样的语句:

  1. if(aBox < 20.0)  
  2. // Do something... 

函数不会处理这里的表达式。如果试图使用比较CBox对象与数值的表达式,那么将得到一条错误消息。为了支持该功能,需要编写另一个版本的operator<()函数作为重载函数。

要支持刚刚看到的表达式类型非常容易。类内的成员函数定义如下所示:

  1. // Function to compare a CBox object with a constant  
  2. bool CBox::operator<(const double& value) const  
  3. {  
  4. return this->Volume() < value;  

<运算符的右操作数对应于这里的函数形参。作为左操作数的CBox对象是由隐式指针this传递的。没有比这更简单的事情了,不是吗?但使用<运算符处理CBox对象仍然存在问题。我们希望写出下面这样的语句:

  1. if(20.0 < aBox)  
  2. // do something... 

有人可能认为,实现接受double类型右实参的operator>()运算符函数,然后相应重写上面这条语句,同样可以完成相同的功能,这么说非常正确。实际上无论如何,实现>运算符都是比较CBox对象所必需的。但是,在实现对某种对象类型的支持时,不应该人为地限制在表达式中使用这种对象的方式。对象的使用应该尽可能自然。现在的问题是如何来做。

成员运算符函数总是以左边的实参作为指针this。因为本例中左边的实参是double类型,所以不能以成员函数的形式实现该运算符。剩下的只有两种选择:普通函数或友元函数。因为不需要访问CBox类的private成员,所以该函数不必是友元函数。这样可以将左操作数属于double类型的重载<运算符实现为普通函数,如下所示:

  1. // Function comparing a constant with a CBox object  
  2. inline bool operator<(const double& value, const CBox& aBox)  
  3. {  
  4. return value < aBox.Volume();  

如前所述,普通函数(就这一点而论也包括友元函数)使用直接成员选择运算符和对象名访问对象的公有成员。成员函数Volume()是公有的,因此这里使用该函数没有问题。

如果CBox类没有公有函数Volume(),可以直接将运算符函数声明为能够直接访问私有数据成员的友元函数,或者提供一组返回私有数据成员数值的成员函数,然后在普通函数中使用这些函数来实现比较功能。

还有另一种方式。我们需要用到>、>=、<=和!=运算符。读者可以自己实现这些运算符,也可以使用标准库所提供的。Utility头文件为运算符函数定义了一组模板,如下所示:

  1. template <class T> bool operator!=(const T& x, const T& y); // Requires ==  
  2. template <class T> bool operator>(const T& x, const T& y); // Requires < 
  3. template <class T> bool operator<=(const T& x, const T& y); // Requires < 
  4. template <class T> bool operator>=(const T& x, const T& y); // Requires < 

这些模板会给任意类创建比较运算符函数。上面的注释说明必须实现operator<()和operator==(),这些模板才能用于类。这些模板在std::rel_ops名称空间中定义,所以可以在源文件中使用下面的using指令启用这些模板:

  1. using namespace std::rel_ops; 

有了上述指令,就可以对类自由运用这4个附加的运算符函数。下面就试一试。

试一试:完成>比较运算符的完全重载

可以在示例中将这些放在一起以说明实际的工作过程:

  1. // Ex8_04.cpp  
  2. // Implementing the comparison operators  
  3. #include <iostream>                                                                     // For stream I/O  
  4. #include <utility>                                                                          // For operator overload templates  
  5. using std::cout;  
  6. using std::endl;  
  7. using namespace std::rel_ops;  
  8. class CBox                                                                                                      // Class definition at global scope  
  9. {  
  10. public:  
  11. // Constructor definition  
  12. explicit CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0):  
  13. m_Length(lv), m_Width(wv), m_Height(hv)  
  14. {  
  15. cout << endl << "Constructor called.";  
  16. }  
  17. // Function to calculate the volume of a box  
  18. double Volume() const  
  19. {  
  20. return m_Length*m_Width*m_Height;  
  21. }  
  22. // Operator function for 'less than' that  
  23. // compares volumes of CBox objects.  
  24. bool operator<(const CBox& aBox) const  
  25. {  
  26. return this->Volume() < aBox.Volume();  
  27. }  
  28. // 'Less than' operator function to compare a CBox object volume with a constant  
  29. bool operator<(const double& value) const  
  30. {  
  31. return this->Volume() < value;  
  32. }  
  33. // 'Greater than' function to compare a CBox object volume with a constant  
  34. bool operator>(const double& value) const  
  35. {  
  36. return this->Volume() > value;  
  37. }  
  38. // Overloaded equality operator  
  39. bool operator==(const CBox& aBox) const  
  40. {  
  41. return this->Volume() == aBox.Volume();  
  42. }  
  43. // Destructor definition  
  44. ~CBox()  
  45. { cout << "Destructor called." << endl;}  
  46. private:  
  47. double m_Length;                                                                            // Length of a box in inches  
  48. double m_Width;                                                                             // Width of a box in inches  
  49. double m_Height;                                                                            // Height of a box in inches  
  50. };  
  51. // Function comparing a constant with a CBox object  
  52. inline bool operator<(const double& value, const CBox& aBox)  
  53. {  
  54. return value < aBox.Volume();  
  55. }  
  56. int main()  
  57. {  
  58. CBox smallBox(4.0, 2.0, 1.0);  
  59. CBox mediumBox(10.0, 4.0, 2.0);  
  60. CBox otherBox(2.0, 1.0, 4.0);  
  61. if(mediumBox != smallBox)  
  62. cout << endl << "mediumBox is not equal to smallBox";  
  63. if(mediumBox > smallBox)  
  64. cout << endl << "mediumBox is bigger than smallBox";  
  65. else  
  66. cout << endl << "mediumBox is not bigger than smallBox";  
  67. if(otherBox >= smallBox)  
  68. cout << endl << "otherBox is greater than or equal to smallBox";  
  69. else  
  70. cout << endl << "otherBox is smaller than smallBox";  
  71. if(otherBox >= mediumBox)  
  72. cout << endl << "otherBox is greater than or equal to mediumBox";  
  73. else  
  74. cout << endl << "otherBox is smaller than mediumBox";  
  75. if(mediumBox > 50.0)  
  76. cout << endl << "mediumBox capacity is more than 50";  
  77. else  
  78. cout << endl << "mediumBox capacity is not more than 50";  
  79. if(10.0 < smallBox)  
  80. cout << endl << "smallBox capacity is more than 10";  
  81. else  
  82. cout << endl << "smallBox capacity is not more than 10";  
  83. cout << endl;  
  84. return 0;  
  85. }  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇8.4.1 实现重载的运算符(2) 下一篇8.4.2 实现对比较运算符的完全支..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·python数据分析岗的 (2025-12-25 10:02:21)
·python做数据分析需 (2025-12-25 10:02:19)
·成为一个优秀的pytho (2025-12-25 10:02:16)
·Java后端面试实习自 (2025-12-25 09:24:21)
·Java LTS版本有哪些 (2025-12-25 09:24:18)