Indeed!
By returning a reference of a scoped variable you're incrementing it's "reference-counter", such that the variable is not destroyed when leaving the scope.
The variable will be destroyed as soon as you also delete any variable holding that reference, cause the reference counter goes to 0.
Can't give much guidance about how C or C++ behave.
Cheers Rolf
( addicted to the Perl Programming Language)
from perlref
Hard references are smart--they keep track of reference counts for you,
automatically freeing the thing referred to when its reference count
goes to zero. (Reference counts for values in self-referential or
cyclic data structures may not go to zero without a little help; see
"Two-Phased Garbage Collection" in perlobj for a detailed explanation.)
If that thing happens to be an object, the object is destructed. See
perlobj for more about objects. (In a sense, everything in Perl is an
object, but we usually reserve the word for references to objects that
have been officially "blessed" into a class package.)
In reply to Re: my and scope
by LanX
in thread my, scope, and references
by chayashida
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |