lundi 26 février 2018

The exact precedence of actions the compiler does to a stack frame?

I have this code:-

int i;
class A
{
public:
    ~A()
    {
        i = 10;
    }
};

int& foo()
{
    i = 3;
    A ob;
    return i;
}

int main()
{
    cout << "i = " << foo() << endl;
    return 0;
}

The output is 10, as the destructor updates the value of foo() i.e i. My question is, shouldnt foo() not exist anymore since the function is out of scope ? Or is foo i.e the return value stored somewhere in memory ? If so then how long is it stored ? I want to know the actions the compiler does in order.

Thankyou.

Aucun commentaire:

Enregistrer un commentaire