Effective C++ 笔记 —— Item 21: Don't try to return a reference when you must return an object.

Consider a class for representing rational numbers, including a function for multiplying two rationals together:

class Rational 
{
public:
    Rational(int numerator = 0, int denominator = 1);  // see Item 24 for why this constractor isn’t declared explicit
    // ...
private:
    int n, d; // numerator and denominator
    friend const Rational operator*(const Rational& lhs, const Rational& rhs); // see Item 3 for why the return type is const
};

A function can create a new object in only two ways: on the stack or on the heap.

const Rational& operator*(const Rational& lhs, const Rational& rhs) // warning! bad code!
{
    Rational result(lhs.n * rhs.n, lhs.d * rhs.d);
    return result;
}

because it has been destroyed. Any caller so much as glancing at this function’s return value would instantly enter the realm of undefined behavior.

const Rational& operator*(const Rational& lhs, const Rational& rhs) // warning! more bad code!
{
    Rational *result = new Rational(lhs.n * rhs.n, lhs.d * rhs.d);
    return *result;
}

who will apply delete to the object conjured up by your use of new?

 

Even if callers are conscientious and well intentioned, there’s not much they can do to prevent leaks in reasonable usage scenarios like this:

Rational w, x, y, z;
w = x * y * z; // same as operator*(operator*(x, y), z)

Here, there are two calls to operator* in the same statement, hence two uses of new that need to be undone with uses of delete. Yet there is no reasonable way for clients of operator* to make those calls, because there’s no reasonable way for them to get at the pointers hidden behind the references being returned from the calls to operator*. This is a guaranteed resource leak.

 

The right way to write a function that must return a new object is to have that function return a new object. For Rational’s operator*, that means either the following code or something essentially equivalent:

inline const Rational operator*(const Rational& lhs, const Rational& rhs)
{
  return Rational(lhs.n * rhs.n, lhs.d * rhs.d);
}

 

Things to Remember:

  • Never return a pointer or reference to a local stack object, a reference to a heap-allocated object, or a pointer or reference to a local static object if there is a chance that more than one such object will be needed. (Item 4 provides an example of a design where returning a reference to a local static is reasonable, at least in single-threaded environments.)

 

posted @ 2021-09-07 16:35  MyCPlusPlus  阅读(60)  评论(0编辑  收藏  举报