Magcialking:
Like this:
Type& a=b();
I wonder if the value b returned can be refered this way.
Firstly, it depends on:
(1) Whether "Type" is a const type or not, e.g. typedef int const Type;
Secondly, it depends on:
(1) Whether the function returns by value or by reference.
If the function returns by reference, then it also depends on:
(1) The lifetime of the object to which it refers.
First thing you'll notice is that you can't bind a "reference to non-
const" to a function invocation which returns by value (or any R-value for
that matter).
Second thing you'll notice is that if the function returns by reference,
then you better make sure that the referred object is still valid after the
function returns. Here's a few examples of good and bad:
int Func1() { return 5; }
int &Func2() { int i = 5; return i; }
int const &Func3() { int const i = 5; return i; }
int main()
{
int &r1 = Func1(); /* Compiler ERROR */
int const &r2 = Func1(); /* OK! */
int &r3 = Func2(); /* Object already destroyed! */
int const &r4 = Func2(); /* Object already destroyed! */
int &r5 = Func3(); /* Compiler ERROR */
int const &r6 = Func3(); /* Object already destroyed! */
}
--
Frederick Gotham