P
PeteUK
Hello,
I'm doing a code review for someone and picked them up on this:
BigClass AFunction() { /*returns a large object by value*/ }
Caller()
{
BigClass bc = AFunction();
/* use bc */
}
I saif they should do this in the caller to avoid copying the
temporary:
Caller()
{
const BigClass& bc = AFunction();
/* use bc */
}
They came back with the following code which compiled but I asked them
to change to const but I had trouble justifying why it should be
const:
Caller()
{
BigClass& bc = AFunction();
/* use bc */
}
Does the life of the temporary get extended using the non-const
reference as it does from using the const reference? Was I right to
pick him up on it not being const? Please give me some rationale if
I'm right!
Thanks,
PeteUK
I'm doing a code review for someone and picked them up on this:
BigClass AFunction() { /*returns a large object by value*/ }
Caller()
{
BigClass bc = AFunction();
/* use bc */
}
I saif they should do this in the caller to avoid copying the
temporary:
Caller()
{
const BigClass& bc = AFunction();
/* use bc */
}
They came back with the following code which compiled but I asked them
to change to const but I had trouble justifying why it should be
const:
Caller()
{
BigClass& bc = AFunction();
/* use bc */
}
Does the life of the temporary get extended using the non-const
reference as it does from using the const reference? Was I right to
pick him up on it not being const? Please give me some rationale if
I'm right!
Thanks,
PeteUK