FAQ 32.03 What does the compiler mean by the warning Returning a reference to a local object ?

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.



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net