#include <iostream>
using namespace std;
int i; //1 - global
class Test
{
public:
~Test()
{
i = 10;
}
};
int& foo()
{
int i = 3; //2 - local
Test ob;
return i;
}
int main()
{
cout << "i = " << foo() << endl; // output: i = 3
return 0;
}
I have queries for above code:
Variable i being local to foo, reference of which cannot be used (automatic variable). How does above code execute?
The Test object in foo function will be destroyed after return statement. How does foo function returns reference of 2 (i = 3)?