Tuesday, April 6, 2010

Returning a Pointer to Local Variable from a Function

It is bad to return a pointer to a local variable from a function, because the pointer will be dangling when the function returns.

It is ok to return a const reference to a const defined in a function, because the compiler will ensure the lifetime of the const will be at least as long as the lifetime of the const reference.

No comments:

Post a Comment