Help with logic

Feb 22, 2013 at 6:45am
closed account (jGzURXSz)
I have been trying to figure this out and have been unsuccessful. I was wondering if anyone could help me understand this.


"Why does the following code display 5 and not 10? How can you change funct() so that the code displays 10 instead of 5? You can ONLY add to (not remove any part of) funct(). You cannot change the statement i=5; to i=10;"


int i; //global variable
class Y
{
public:
~Y()
{
i=10;
}
};
int funct()
{
i=5;
Y y;
return i;
}
void main()
{
cout << "i = "<<funct()<<"\n";
}
Feb 22, 2013 at 7:07am
It seems an object of type Y must be destroyed before the statement return i;
There may be a few different ways to make that happen.
Topic archived. No new replies allowed.