第11章 使用类——运算符重载(二)运算符重载限制

本文章是作者根据史蒂芬·普拉达所著的《C++ Primer Plus》而整理出的读书笔记,如果您在浏览过程中发现了什么错误,烦请告知。另外,此书由浅入深,非常适合有C语言基础的人学习,感兴趣的朋友可以自行阅读此书籍。

运算符重载限制

可以被重载的运算符有:

+

-

*

/

%

^

&

|

~=

!

=

<

>

+=

-=

*=

/=

%=

^=

&=

|=

<<

>>

>>=

<<=

==

!=

<=

>=

&&

||

++

--

,

->*

->

()

[]

new

delete

new[]

delete

以下是C++对用户定义的运算符重载的限制:

重载后的运算符必须至少有一个操作数是用户定义的类型,这将防止用户为标准类型重载运算符。

例如,不能将减法运算符(-)重载为计算两个double值得和。

使用运算符时不能违反运算符原来的句法规则。

例如,不能将求模运算符(%)重载成使用一个操作数。

不能创建新运算符。

例如,不能定义operator**()函数来表示求幂。

不能重载下面的运算符。

sizeof:sizeof运算符。

不能重载的运算符

说明

sizeof

sizeof运算符

.

成员运算符

.*

成员指针运算符

::

作用域解析运算符

?:

条件运算符

typeid

一个RTTI运算符

const_cast

强制类型转换运算符

dynamic_cast

强制类型转换运算符

reinterpret_cast

强制类型转换运算符

static_cast

强制类型转换运算符

大多数运算符都可以通过成员或非成员函数进行重载,但下面的运算符只能通过成员函数进行重载。

只能通过成员函数重载的运算符

说明

=

赋值运算符

()

函数调用运算符

[]

下标运算符

->

通过指针访问类成员的运算符

对上一篇文章所设计的类,我们可以继续完善,比如,有可能我们需要将两个时间相减或将时间乘以一个因子,这需要重载减法和乘法运算符。

因此在Time类中添加如下两个方法:

Time operator-(const Time& t) const;

Time operator*(double n) const;

类声明如下:

//mytime2.hpp

#ifndef _MYTIME0_HPP_

#define _MYTIME0_HPP_

class Time

{

private:

int hours;

int minutes;

public:

Time();

Time(int h, int m = 0);

void AddMin(int m);

void AddHr(int h);

void Reset(int h = 0, int m = 0);

Time operator+(const Time& t) const;

Time operator-(const Time& t) const;

Time operator*(double n) const;

void Show() const;

};

#endif

类方法实现如下:

//mytime2.cpp

#include

#include "mytime2.hpp"

Time::Time()

{

hours = minutes = 0;

}

Time::Time(int h, int m)

{

hours = h;

minutes = m;

}

void Time::AddMin(int m)

{

int total_minutes = minutes + m;

minutes = total_minutes % 60;

hours += total_minutes / 60;

}

void Time::AddHr(int h)

{

hours += h;

}

void Time::Reset(int h, int m)

{

hours = h;

minutes = m;

}

Time Time::operator+(const Time& t) const

{

Time tmp;

int total_minutes = t.minutes + minutes;

tmp.minutes = total_minutes % 60;

tmp.hours += hours + t.hours + total_minutes / 60;

return tmp;

}

Time Time::operator-(const Time& t) const

{

Time tmp;

int result = (minutes + hours*60) - (t.minutes + t.hours*60);

if (result > 0)

{

tmp.hours = result / 60;

tmp.minutes = result % 60;

}

return tmp;

}

Time Time::operator*(double n) const

{

Time tmp;

int total_minutes = minutes + hours * 60;

int result = total_minutes * n;

tmp.hours = result / 60;

tmp.minutes = result % 60;

return tmp;

}

void Time::Show() const

{

std::cout << hours << " hours " << minutes << " minutes" << std::endl;

}

使用类:

//usetime2.cpp

#include

#include "mytime2.hpp"

int main()

{

using std::cout;

using std::endl;

Time time_work;

Time time_work_morning(2, 35);

Time time_work_afternoon(4, 40);

Time time_work_evening(2, 30);

cout << "morning work time: ";

time_work_morning.Show();

cout << endl;

cout << "afternoon work time: ";

time_work_afternoon.Show();

cout << endl;

cout << "evening work time: ";

time_work_evening.Show();

cout << endl;

time_work = time_work_morning + time_work_afternoon+ time_work_evening;

cout << "total time: ";

time_work.Show();

cout << endl;

time_work = time_work * 5;

cout << "five days total time: ";

time_work.Show();

cout << endl;

time_work = time_work_afternoon - time_work_morning;

cout << "time_work(afternoon - morning) time: ";

time_work.Show();

cout << endl;

return 0;

}

程序结果如下:

morning work time: 2 hours 35 minutes

afternoon work time: 4 hours 40 minutes

evening work time: 2 hours 30 minutes

total time: 9 hours 45 minutes

five days total time: 48 hours 45 minutes

time_work(afternoon - morning) time: 2 hours 5 minutes