FAQ 32.03 What does the compiler mean by the warning "Returning a reference to a local object"?It means "Pay attention to me or you'll regret it!" A local (auto) object is an object local to a routine (and it is usually allocated on the stack). Never return a reference or a pointer to a local (auto) object. As soon as the function returns, the local object is destructed, and the reference or pointer refers to garbage. A program working with garbage eventually gets very, very sick. Note that returning a copy of a local object (returning "by value") is fine. |