const Rational& operator* (const Rational& lhs,
const Rational& lhs)
{
Rational result(lhs.n * rhs.n, lhs.d* rhs.d)
return result;
}
|
此函式傳回一個指向local的物件,此物件在函式退出前就被銷毀了,若再針對此物件做處理,就會發生不可預期之錯誤。
◎結論
絕不要傳回pointer或reference指向一個local stack物件,或傳回reference指向一個heap-allocated物件,或傳回pointer或reference指向一個local static 物件而有可能同時需要多個這樣的物件。
沒有留言 :
張貼留言