This is a two part question. Is it ok to assign the return value of a function to a reference? Such as
Foo FuncBar()
{
return Foo();
}
// some where else
Foo &myFoo = FuncBar();
Is this ok? Its my understanding that FuncBar()
returns a Foo object and now myFoo
is a reference to it.
Second part of the question. Is this an optimization? So if your doing it in a loop a lot of the time is it better to do
Foo &myFoo = FuncBar();
or
Foo myFoo = FuncBar();
And take into account the variables use, won't using the ref require slower dereferences?