mardi 29 octobre 2019

Why is reference type a lvalue when accessed with temporary object

Why assigning a value to a reference variable being accessed using temporary object works but not for non reference type?

class a
{
    public:
        int m;
        int &n;
        a():m(2),n(m)
        {
            cout<< "A's constructor"<<endl;
        }
};

int main()
{
    // a().m = 6; // this gives an error that temporary object being used 
                  // as lvalue
    a().n = 20;   // But this line works

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire