首页 > 代码库 > Effective C++:条款21:必须返回对象时别妄想返回其reference

Effective C++:条款21:必须返回对象时别妄想返回其reference

(一)

一定要避免传递一些references去指向其实并不存在的对象。

看下面这个类:

class Rational { 
public: 
    Rational(int numerator = 0, int denominator = 1); 
private: 
    int n, d; 
    friend const Rational operator*(const Rational& lhs, const Rational& rhs); 
};
这个函数不能返回引用,


(二)在stack中分配local对象:

const Rational& operator*(const Rational& lhs, const Rational& rhs) 
{ 
    Rational result(lhs.n * rhs.n, lhs.d * rhs.d); //警告!糟糕的代码! 
    return result; 
}
这是错误的!
你的目标是要避免调用构造函数,而result却必须像任何对象一样地由构造函数构造起来。更严重的是result是一个local对象,而local对象在函数退出前被销毁了。因此这个返回的reference指向的是一个“从前的”Rational;如今已经被销毁了。任何调用者甚至对此函数的返回值做任何一点点运用,都会陷入“无定义行为”的恶地。

(三)在heap中分配对象:

const Rational& operator*(const Rational& lhs, const Rational& rhs) 
{ 
    Rational* result = new Rational(lhs.n * rhs.n, lhs.d * rhs.d); //更糟的代码! 
    return *result; 
}
这是错误的!谁该对着被你new出来的对象实施delete?没有合理的办法让他们取到operator*返回的reference背后隐藏的那个指针。这绝对导致资源泄漏。


(四)让operator*返回reference指向一个被定义于函数内部的static Rational对象

const Rational& operator*(const Rational& lhs, const Rational& rhs) { 
    static Rational result; //又是一堆烂代码。 
    result = ...; //lhs*rhs 
    return result; 
}
当用户写下如下代码时:

bool operator=(const Rational& lhs, const Rational& rhs); 
Rational a, b, c, d; 
if ((a*b) == (c*d)){ 
    ... 
}else{ 
    ... 
}
表达式if ((a*b) == (c*d))总是被核算为true, 不论a,b, c,d是什么。因为虽然两次调用operator*都改变了static Rational对象的值, 但是返回的reference, 调用端看到的永远是static Rational的现值。

所以:欲令诸如operator*这样的函数返回reference,只是浪费时间而已。

(六)一个“必须返回新对象”的函数的正确写法:就让那个函数返回一个新对象。

inline const Rational operator*(const Rationa& lhs, const Rational& rhs){

    return Rational(lhs.n * rhs.n, lhs.d * rhs.d);

}


请记住:
绝不要返回pointer或reference指向一个local stack对象, 或返回reference指向一个heap-allocated对象, 或返回pointer或reference指向一个local static对象而有可能同时需要多个这样的对象。条款4已经为“在单线程环境中合理返回reference指向一个local static对象”提供了一份设计实例(singleton)