”operator=”其实是一个函数名

来源:互联网 发布:网络电信诈骗嫌犯 编辑:程序博客网 时间:2024/05/02 01:55

1. 以前,一直不太明白,重载运算符,今天看了《C++高级编程,第8章》,终于明白了。


”operator=” 其实是一个函数,关于拷贝构造函数,之前的博客已经写过,当时以为是一个套路。


现在终于明白,其实是一个函数,而这个函数 返回了一个 类类型的引用,这个函数的参数是类类型的引用。例子如下:


Spreadsheet& operator=(const Spreadsheet& rhs);//rhs是形参,返回值是这个类的引用


2. 明白了这个,也就能够理解,运算符重载的意思了。


SpreadsheetCell operator+(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs){SpreadsheetCell newCell;newCell.set(lhs.mValue + rhs.mValue);return newCell;}


很好理解了,对吧。

=========




0 0
原创粉丝点击