P
pauldepstein
#include <iostream>
using namespace std;
double & GetWeeklyHours()
{
double h = 46.50;
double &hours = h;
return hours;
}
//---------------------------------------------------------------------------
int main()
{
double hours = GetWeeklyHours();
cout << "Weekly Hours: " << hours << endl;
return 0;
}
According to a (hopefully reliable) website, the above is correct
code.
Why is the above _not_ an example of the sin of "returning a reference
to a local variable"? What is the difference between the return-
reference-to-local problem and the above code?
Thanks,
Paul Epstein
using namespace std;
double & GetWeeklyHours()
{
double h = 46.50;
double &hours = h;
return hours;
}
//---------------------------------------------------------------------------
int main()
{
double hours = GetWeeklyHours();
cout << "Weekly Hours: " << hours << endl;
return 0;
}
According to a (hopefully reliable) website, the above is correct
code.
Why is the above _not_ an example of the sin of "returning a reference
to a local variable"? What is the difference between the return-
reference-to-local problem and the above code?
Thanks,
Paul Epstein