Effective C++ item 21: Don't try to return a reference when you must return an object.

Once programmers grasp the efficiency implications of pass-by-value for objects (see Item 20), many become crusaders, determined to root out the evil of pass-by-value wherever it may hide. Unrelenting in their pursuit of pass-by-reference purity, they invariably make a fatal mistake: they start to pass references to objects that don’t exist. This is not a good thing. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Rational
{
public:
Rational (int numerator = 0, int denominator = 1);
...
private:
int n, d;

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

In the code above, we defined operator* returning a const reference of Rational instead of a const value of Rational which is what you should do. Because result is a local object  which is destroyed when the function exists.

The same rule applies to a pointer to a local stack object, a reference to a heap-allocated object or a pointer or reference to a local static object. Think twice before returning a reference of these objects, most of time it will give you trouble.

Reference:
“Effective C++” Third Edition by Scott Meyers.