运算符重载(附源代码)

运算符重载

重载意义

在C++预定义中,运算符的操作对象只局限于基本的内置数据类型(int、double…),但是对于我们自定义的类型(类)是无法完成对应操作的。当我们对自定义的类型也有类似操作需求时,便需要对运算符进行不同的定义,赋予其新的功能,通过参数的类型及个数来区分,完成重载。

重载格式:
<返回类型说明符> operator <运算符符号>(<参数表>)
{

 <函数体>

}

重载限制

重载运算符的基本原则

  • C++规定重载后的运算符的操作对象必须至少有一个是用户定义的类型
  • 使用运算符时不能违反运算符原来的句法规则。

例如:不能将求模运算符(%)重载成使用一个操作数;不能修改运算符的优先级等…

  • 不能创建新的运算符。

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

  • 不能重载下面的运算符:

.* //成员指针运算符
:: //作用域解析运算符
sizeof //sizeof运算符
?: //条件运算符
. //成员运算符

  • 以下运算符只能通过成员函数进行重载

= //赋值运算符
() //函数调用运算符
[] //下标运算符
-> //通过指针访问类成员的运算符

为何=、()、[ ]、->只能通过成员函数重载:
原因很简单,会和编译器默认分配的运算符重载成员函数冲突,引发歧义。我们知道友元函数不是类的成员函数,它只是类的“朋友“,具有访问把它声明为“朋友”的类的数据成员的权限而已。那么当把赋值运算符重载为类的友员函数,在程序中执行类对象的赋值语句时,程序就会出现两种矛盾的选择。因为它认为类中并没有重载赋值运算符的成员函数,所以它根据C++的规则,会去调用相应的构造函数。但是在全局里,我们已经重载了参数类型为此类类型的赋值运算符函数,而这赋值语句刚好和这函数匹配上了,根据C++的规则,也会去调用这函数。故出现二义性。

重载运算符代码

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
visual basic 2005 技术内部中第六章第七节运算符重载代码。 operator部分: Module Module1 Sub Main() End Sub End Module Public Structure Fraction 'Read-Only fields Private num As Long Private den As Long 'Read-Only properties Public ReadOnly Property Numerator() As Long Get Return num End Get End Property Public ReadOnly Property Denominator() As Long Get Return den End Get End Property Sub New(ByVal numerator As Long, ByVal denominator As Long) 'Normalize the numerator and denominator If numerator = 0 Then numerator = 1 ElseIf denominator < 0 Then numerator = -numerator denominator = -denominator End If Dim div As Long = GCD(numerator, denominator) num = numerator \ div den = denominator \ div End Sub 'the greatest common divisor of two numbers (helper method) Private Function GCD(ByVal n1 As Long, ByVal n2 As Long) As Long n1 = Math.Abs(n1) n2 = Math.Abs(n2) Do 'ensure that n1>n2 If n1 < n2 Then Dim tmp As Long = n1 n1 = n2 n2 = tmp End If n1 = n1 Mod n2 Loop While n1 <> 0 End Function 'override ToString to provide a textual representation of the fraction Public Overrides Function ToString() As String If num = 0 OrElse den = 1 Then Return num.ToString Else Return String.Format("{0}/{1}", num, den) End If End Function Public Shared Operator +(ByVal f1 As Fraction, ByVal f2 As Fraction) As Fraction 'a/b+c/d=(a*d+b*c)/(b*d) Return New Fraction(f1.num * f2.den + f2.num * f1.den, f1.den * f2.den) End Operator Public Shared Operator -(ByVal f1 As Fraction, ByVal f2 As Fraction) 'a/b-c/d=(a*d-b*c)/(b*d) Return New Fraction(f1.num * f2.num, f1.den * f2.den) End Operator Public Shared Operator *(ByVal f1 As Fraction, ByVal f2 As Fraction) 'a/b * c/d=(a*c)/(b*d) Return New Fraction(f1.num * f2.num, f1.den * f2.den) End Operator Public Shared Operator /(ByVal f1 As Fraction, ByVal f2 As Fraction) Return New Fraction(f1.num * f2.den, f1.den * f2.num) End Operator End Structure

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值