By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

Is it safe to refer to function-returned values?

P: n/a
Like this:

Type& a=b();

I wonder if the value b returned can be refered this way.

Nov 8 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* Magcialking:
Like this:

Type& a=b();

I wonder if the value b returned can be refered this way.
Depends on the definition of Type and the function b().

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 8 '06 #2

P: n/a
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
Nov 8 '06 #3

P: n/a
Frederick Gotham:
int const &r2 = Func1(); /* OK! */

I should have explained this better. The reason why this is OK is that C++
has special rules for binding a "reference to const" to an R-value.
Specifically, the following:

int const &r = 5;

should behave as if it were:

int const rval = 5;
int const &r = rval;

The return value from a function which returns by value is an R-value.

--

Frederick Gotham
Nov 8 '06 #4

P: n/a
Thanks so mush Gotham, this really helped a lot.

"Frederick Gotham д
"
Frederick Gotham:
int const &r2 = Func1(); /* OK! */


I should have explained this better. The reason why this is OK is that C++
has special rules for binding a "reference to const" to an R-value.
Specifically, the following:

int const &r = 5;

should behave as if it were:

int const rval = 5;
int const &r = rval;

The return value from a function which returns by value is an R-value.

--

Frederick Gotham
Nov 9 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.