类的operator +操作符满足交换律

例如Spreadsheetcell(单元格)

      若operator +作为类的成员函数,因为operator +方法必须Spreadsheetcell对象上调用,对象必须在operator +的左边,

若遇到athirdCell= mycell+4;可以编译,但是athirdCell=4+mycell则通不过编译。

想要解决这个问题,需要通过全局的operator +

const  Spreadsheetcell operator +(const  Spreadsheetcell &lhs , const  Spreadsheetcell& rhs)

{

        Spreadsheetcell   newCell;

        newCell.set(lhs.mValue+rhs.mValue);

        return newCell;

}


若operator +访问了Spreadsheetcell Spreadsheetcell operator +需要时Spreadsheetcell 类的友元函数;

class  Spreadsheetcell 

{

public :

    friend  const  Spreadsheetcell operator +(const  Spreadsheetcell &lhs , const  Spreadsheetcell& rhs);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值