I've been playing around with references (I'm still having issues in this regard).
1- I would like to know if this is an acceptable code:
int & foo(int &y)
{
return y; // is this wrong?
}
int main()
{
int x = 0;
cout << foo(x) << endl;
foo(x) = 9; // is this wrong?
cout << x << endl;
return 0;
}
2- Also this is from an exam sample:
Week & Week::highestSalesWeek(Week aYear[52])
{
Week max = aYear[0];
for(int i = 1; i < 52; i++)
{
if (aYear[i].getSales() > max.getSales())
max = aYear[i];
}
return max;
}
It asks about the mistake in this code, also how to fix it.
My guess is that it return a local reference. The fix is:
Week & max = aYear[0];
Is this correct/enough?