C++运算符重载

技术总结:
运算符重载,目的就是让自定义的类可以使用 运算符。
具体运算操作,写在重载的运算符操作内。

前置资料

运算符重载

要重载运算符,需使用被称为运算符函数的特殊函数形式。运算符函数的格式如下:

示例解析
operatorop(argument-list)operator + 运算符 + (参数列表)
operator+( )重载+运算符
operator*( )重载*运算符
operator-( )重载-运算符
重载限制:
  • op必须是有效的C++运算符,不能虚构一个新的符号。
  • 重载后的运算符必须至少有一个操作数是用户定义的类型,这将防止用户为标准类型重载运算符。(一定其中一个是自定义类,一般都是自定义类运算符重载)
  • 使用运算符时不能违反运算符原来的句法规则。(比如运算符通常是要两个操作数a+b 之类)
  • 不能修改运算符的优先级。(运算优先级与原先一致)
  • 不能重载下面的运算符
运算符解析
sizeofsizeof运算符
.成员运算符
. *成员指针运算符
::作用域解析运算符
?:条件运算符
typeid一个RTTI运算符
const_cast强制类型转换运算符
dynamic_cast强制类型转换运算符
reinterpret_cast强制类型转换运算符
static_cast强制类型转换运算符
  • 大多数运算符都可以通过成员或非成员函数进行重
    载,但下面的运算符只能通过成员函数进行重载。
运算符解析
=赋值运算符
( )函数调用运算符
[ ]下标运算符
->通过指针访问类成员的运算符
  • 其他可重载运算符
+-*/%^
&|~=!=<
>+=-=*=/=%=
^=&=|=<<>>>>=
<<===!=<=>=&&
||++- -,->*->
( )[ ]newdeletenew[ ]delete[ ]

友元函数

关键字:friend
在一个类中,使用关键字 friend + 函数,即可让函数成为类的友元函数。

特性

C++ 提供的外部对类的private成员的访问方式
通过让函数成为类的友元,可以赋予该函数与类的成员函数相同的访问权限

友元家族
  • 友元函数
  • 友元类
  • 友元成员函数

实例解析

Time.h

#include <iostream>
class Time{
private:
    int hours;
    int minutes;
public:
    Time();
    Time(int h, int m = 0);
    Time operator*(const int n);										#1
    friend Time operator+(const Time &t1, const Time &t2);
    friend Time operator-(const Time &t1, const Time &t2);
    friend Time operator*(const double n, const Time &t1);				#2
    friend std::ostream& operator<<(std::ostream &os, const Time &t);
};

Time.cpp

int hours;
int minutes;
Time::Time(void)
{
    hours = 0;
    minutes = 0;
}
Time::Time(int h, int m)
{
    hours = h;
    minutes = m;
}
Time Time::operator*(const int n)
{
    hours = hours * n + (minutes * n) / 60;
    minutes = (minutes * n) % 60;
    return *this;
}

//下面开始是友元函数
Time operator+(const Time &t1, const Time &t2)								#3
{
    Time t3;
    t3.minutes = t1.minutes + t2.minutes;
    t3.hours = t1.hours + t2.hours + t3.minutes / 60;
    t3.minutes = t3.minutes % 60;
    return t3;
}
Time operator-(const Time &t1, const Time &t2)
{
    Time diff;
    int tot1, tot2;
    tot1 = t1.hours * 60 + t1.minutes;
    tot2 = t2.hours * 60 + t2.minutes;
    diff.hours = (tot1 - tot2) / 60;
    diff.minutes = (tot1 - tot2) % 60;
    return diff;
}
Time operator*(const double n, const Time &t1)
{
    Time result;
    long totalminutes = t1.hours * n * 60 + t1.minutes * n;
    result.hours = totalminutes / 60;
    result.minutes = totalminutes % 60;
    return result;
}
std::ostream& operator<<(std::ostream &os, const Time &t)
{
    os << t.hours << " hours, " << t.minutes << " minutes";
    return os;
}

main.cpp 调用输出

int main(void)
{
    Time aida(3, 35);
    Time tosca(2, 48);
    Time temp;

    cout << "Aida and Tosca:\n";
    cout << aida << "; " << tosca << endl;
    temp = aida + tosca;
    cout << "Aida + Tosca: " << temp << endl;
    temp = aida*1.17;															#4
    cout << "Aida* 1.17: " << temp << endl;
    cout << "10.0 * Tosca: " << 10.0 * tosca << endl;							#5
}
输出为:

Aida and Tosca:
3 hours, 35 minutes; 2 hours, 48 minutes
Aida + Tosca: 6 hours, 23 minutes
Aida* 1.17: 3 hours, 35 minutes
10.0 * Tosca: 28 hours, 0 minutes


解析

  • #1 Timer重载了 * 运算符。 若使用Timer * n 格式的运算,均会调用该重载运算符。

  • #2 Timer利用友元重载了 * 运算符, 若使用了 n * Timer 格式的运算,均会调用该重载运算符。

  • #3 友元函数的基本操作,它首先是普通函数。所以定义实现不需要加关键字friend, 只需要在使用的类中,加上关键字friend即可。

  • Timer * n 与 n * Timer的不同:
    以下运算:

  • #4: 对 aida*1.17 而言,默认类本身为第一参数,所以只需重载Timer * n

  • #5 :而对于 10.0 * tosca , 第一运算符是n,因此无法匹配,需要有 n * Timer运算符的重载。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值