|
|
effective c++中有下面一段代码
- class rational {
- public:
- rational(int numerator = 0, int denominator = 1);
- ...
- private:
- int n, d; // 分子和分母
- friend
- const rational // 参见条款21:为什么
- operator*(const rational& lhs, // 返回值是const
- const rational& rhs)
- };
- inline const rational operator*(const rational& lhs,
- const rational& rhs)
- {
- return rational(lhs.n * rhs.n, lhs.d * rhs.d);
- }
复制代码
这是对的,不过又说operator*这么写就不对
- inline const rational& operator*(const rational& lhs,
- const rational& rhs)
- {
- rational result(lhs.n * rhs.n, lhs.d * rhs.d);
- return result;
- }
复制代码
现在有点迷惑,第二种operator*写法的执行过程大概了解,第一种的过程是如何呢?为什么第种不会出问题?
还有return在什么情况下返回引用? |
|