8.4.4 重载加法运算符
让我们看看如何为CBox类重载加法运算符。这是个有趣的问题,因为涉及创建和返回新的对象。新对象是两个操作数-- 即两个CBox对象的和(无论将和的意义定义成什么)。
那么,我们真正希望两个箱子的和是什么?关于这一点有很多合法的可能性,但我们在这里将力求简单。下面这样定义两个CBox对象的和:它是个CBox对象,其体积足够容纳另外两个码在一起的箱子。通过使新对象的m_Length成员等于两个相加对象中较大的m_Length成员,并以类似的方式求出m_Width,然后使m_Height成员等于两个操作数对象m_Height成员的和,我们即可使合成的CBox对象足以包含另两个CBox对象。这种实现方法未必是最优的解决方案,但就我们的目的而言已经足够。通过修改构造函数,我们还将使CBox对象的m_Length成员总是大于或等于m_Width成员。
CBox类的加法运算符版本更容易通过图形来解释,因此我们进一步在图8-4中阐明了相加的过程。
|
| (点击查看大图)图 8-4 |
因为我们需要直接访问对象的成员,所以应该将operator+()实现为成员函数。该函数成员在类定义内部的声明如下所示:
- CBox operator+(const CBox& aBox) const;
// Function adding two CBox objects
我们将形参定义成引用,以避免该函数被调用时不必要地复制右边的实参。我们还将形参声明为const引用,因为该函数不修改实参。如果不把形参声明为const引用,则编译器将不允许const对象被传递给这个函数,因此+运算符的右操作数不可以是const CBox对象。我们将该运算符函数也声明为const,因为它不修改调用它的那个对象。如果不这样做,则+运算符的左操作数也不可以是const CBox对象。
operator+()函数的定义现在如下所示:
- // Function to add two CBox objects
- CBox CBox::operator+(const CBox& aBox) const
- {
- // New object has larger length and width, and sum of heights
- return CBox(m_Length > aBox.m_Length m_Length:aBox.m_Length,
- m_Width > aBox.m_Width m_Width:aBox.m_Width,
- m_Height + aBox.m_Height);
- }
根据当前对象(*this)和传递的实参对象aBox,我们构造出一个局部的CBox对象。记住,返回过程将创建局部对象的临时副本,并将此副本(而非从函数返回时将被抛弃的局部对象)返回给调用程序。
试一试:练习使用我们重载的加法运算符
在这个示例中,我们将能够看到CBox类中重载加法运算符的工作过程。
- // Ex8_06.cpp
- // Adding CBox objects
- #include <iostream>
// For stream I/O - using std::cout;
- using std::endl;
-
- class CBox
// Class definition at global scope - {
- public:
- // Constructor definition
- CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0): m_Height(hv)
- {
- m_Length = lv > wv lv: wv; // Ensure that
- m_Width = wv < lv wv: lv; // length >= width
- }
-
- // Function to calculate the volume of a box
- double Volume() const
- {
- return m_Length*m_Width*m_Height;
- }
-
- // Operator function for 'greater than' which
- // compares volumes of CBox objects.
- int CBox::operator>(const CBox& aBox) const
- {
- return this->Volume() > aBox.Volume();
- }
-
- // Function to compare a CBox object with a constant
- int operator>(const double& value) const
- {
- return Volume() > value;
- }
-
- // Function to add two CBox objects
- CBox operator+(const CBox& aBox) const
- {
- // New object has larger length & width, and sum of heights
- return CBox(m_Length > aBox.m_Length m_Length:aBox.m_Length,
- m_Width > aBox.m_Width m_Width:aBox.m_Width,
- m_Height + aBox.m_Height);
- }
-
- // Function to show the dimensions of a box
- void ShowBox() const
- {
- cout << m_Length << " "
- << m_Width << " "
- << m_Height << endl;
- }
-
- private:
- double m_Length;
// Length of a box in inches - double m_Width;
// Width of a box in inches - double m_Height;
// Height of a box in inches - };
-
- int operator>(const double& value, const CBox&
aBox); // Function prototype -
- int main()
- {
- CBox smallBox(4.0, 2.0, 1.0);
- CBox mediumBox(10.0, 4.0, 2.0);
- CBox aBox;
- CBox bBox;
-
- aBox = smallBox + mediumBox;
- cout << "aBox dimensions are ";
- aBox.ShowBox();
-
- bBox = aBox + smallBox + mediumBox;
- cout << "bBox dimensions are ";
- bBox.ShowBox();
-
- return 0;
- }
-
- // Function comparing a constant with a CBox object
- int operator>(const double& value, const CBox& aBox)
- {
- return value > aBox.Volume();
- }
在本章后面,我们将再次用到CBox类的定义。因此现在请在书上作好记号,稍后可能需要返回到这里。
示例说明
在该示例中,我们稍微对CBox类的成员作了一些修改。析构函数已经被删除,因为对CBox类而言它不是必需的。构造函数已经被修改成可以保证m_Length成员不小于m_Width成员。知道箱子的长度无论如何也不会小于宽度,将使加法操作更容易些。我们还添加了一个输出CBox对象尺寸的ShowBox()函数。使用该函数,我们将能够验证重载的加法操作是否像预期的那样工作。
该程序的输出如下:
- aBox dimensions are 10 4 3
- bBox dimensions are 10 4 6
该输出看来与前面定义的使CBox对象相加的概念一致。另外可以看出,该函数还可以处理表达式中的多次加法操作。为了计算bBox,重载的加法运算符被调用了两次。
我们也能够以友元函数的形式实现CBox类的加法操作,其原型如下:
- friend CBox operator+(const CBox& aBox, const CBox& bBox);
除需要使用直接成员选择运算符获得函数两个实参的成员以外,得出结果的过程将完全相同。该版本的运算符函数将像第一个版本一样工作。