Z
zebulon
Hi,
I am aware that when a function returns a reference, one should never
return a local variable to that function, since that variable will be
removed from the stack and the reference will be dangling.
Therefore, this is bad:
double& GetWeeklyHours()
{
double h =
46.50;
return h;
}
However, why this example (found in http://www.functionx.com/cpp/examples/returnreference.htm)
seems valid:
double& GetWeeklyHours()
{
double h = 46.50;
double &hours = h;
return hours;
}
Where is the reference &hours created ? Shouldn't it be destroyed when
the function exists too ? Is that ok ? Is that even elegant ?
Thanks for your opinions.
I am aware that when a function returns a reference, one should never
return a local variable to that function, since that variable will be
removed from the stack and the reference will be dangling.
Therefore, this is bad:
double& GetWeeklyHours()
{
double h =
46.50;
return h;
}
However, why this example (found in http://www.functionx.com/cpp/examples/returnreference.htm)
seems valid:
double& GetWeeklyHours()
{
double h = 46.50;
double &hours = h;
return hours;
}
Where is the reference &hours created ? Shouldn't it be destroyed when
the function exists too ? Is that ok ? Is that even elegant ?
Thanks for your opinions.