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

    xiaoxiao2021-12-14  35

    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; }

     

     

     

     

     

    很好理解了,对吧。

    =========

     

     

     

    转载请注明原文地址: https://ju.6miu.com/read-970320.html

    最新回复(0)