运算符重载
可对运算符进行重载,将运算符扩展到自定义类型。
operator【运算符】(argument-list)
例如:operator+()
重载+运算符。【运算符】必须是有效的C++运算符,不能虚构一个新的符号。不能有operator@()
这样的函数,但可以有operator[]()
,因为[]
是数组索引运算符。
实例:计算时间
程序11.1 mytime0.h
#ifndef MYTIME0_H_
#define MYTIME0_H_
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 Sum(const Time &t) const;
void Show() const;
};
#endif // !MYTIME0_H_
Time类提供了用于调整和重新设置时间、显式时间、将两个时间相加的方法。
程序11.2 mytime0.cpp
#include <iostream>
#include "mytime0.h"
Time::Time()
{
hours = minutes = 0;
}
Time::Time(int h, int m)
{
hours = h;
minutes = m;
}
void Time::AddMin(int m)
{
minutes += m;
hours += minutes / 60;
minutes %= 60;
}
void Time::AddHr(int h)
{
hours += h;
}
void Time::Reset(int h, int m)
{
hours = h;
minutes = m;
}
Time Time::Sum(const Time &t) const
{
Time sum;
sum.minutes = minutes + t.minutes;
sum.hours = hours + t.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum;
}
void Time::Show() const
{
std::cout << hours << " hours, " << minutes << " minutes";
}
程序11.3 usetime0.cpp
#include <iostream>
#include "mytime0.h"
int main()
{
using std::cout;
using std::endl;
Time planning;
Time coding(2, 40);
Time fixing(5, 55);
Time total;
cout << "planning time = ";
planning.Show();
cout << endl;
cout << "coding time = ";
coding.Show();
cout << endl;
cout << "fixing time = ";
fixing.Show();
cout << endl;
total = coding.Sum(fixing);
cout << "coding.Sum(fixing) = ";
total.Show();
cout << endl;
system("pause");
return 0;
}
添加加法运算符
程序11.4 mytime1.h
#ifndef MYTIME1_H_
#define MYTIME1_H_
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;
void Show() const;
};
#endif
程序11.5 mytime1.cpp
#include <iostream>
#include "mytime1.h"
Time::Time()
{
hours = minutes = 0;
}
Time::Time(int h, int m)
{
hours = h;
minutes = m;
}
void Time::AddMin(int m)
{
minutes += m;
hours += minutes / 60;
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 sum;
sum.minutes = minutes + t.minutes;
sum.hours = hours + t.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum;
}
void Time::Show() const
{
std::cout << hours << " hours, " << minutes << " minutes";
}
程序11.6 usetime1.cpp
#include <iostream>
#include "mytime1.h"
int main()
{
using std::cout;
using std::endl;
Time planning;
Time coding(2, 40);
Time fixing(5, 55);
Time total;
cout << "planning time = ";
planning.Show();
cout << endl;
cout << "coding time = ";
coding.Show();
cout << endl;
cout << "fixing time = ";
fixing.Show();
cout << endl;
total = coding + fixing;
cout << "coding + fixing = ";
total.Show();
cout << endl;
Time morefixing(3, 28);
cout << "more fixing time = ";
morefixing.Show();
cout << endl;
total = morefixing.operator+(total);
cout << "morefixing.operator+(total) = ";
total.Show();
cout << endl;
system("pause");
return 0;
}
operator+()函数的名称使得可以使用函数表示法或运算符表示法来调用它,编译器将根据操作数的类型来确定如何做。
重载限制
运算符重载的限制:
- 重载后的运算符必须至少由一个操作数是用户定义的类型,这将防止用户为标准类型重载运算符。
- 使用运算符时不能违反运算符原来的句法规则。例如:不能将求模运算符(%)重载成使用一个操作数
int x; % x;//错误
;同样,不能修改运算符的优先级。 - 不能创建新运算符。例如,不能定义operator**()函数来表示求幂。
- 不能重载下面的运算符:sizeof运算符;成员运算符(.);成员指针运算符(.*);条件运算符(?:);typeid;强制类型转换运算符(const_cast 、dynamic_cast、 reinterpret_cast 、static_cast)
- 下面的运算符只能通过成员函数进行重载:赋值运算符(=);函数调用运算符(());下标运算符([]);指针访问类成员运算符(->)。