mardi 27 septembre 2016

Rvalue assignment - primitive vs Class object

I've encountered a change in behaviour (at least to my understanding) between primitives and Classes i've created in c++. say, the following:

int foo()
{
    int a = 4;
    return a;
}

int main() 
{

    foo() = 2;
}

of course creates causes a compilation error, because i'm trying to assign somthing into an rvalue. But if i were to create my own class, say Complex, the following:

Complex foo()
{
    Complex a = Complex(2,4);
    return a;
}

int main() 
{

    foo() = Complex(1,2);
}

would actually work. I thought the only way for this to compile is if foo() returned a reference. What am i missing? i assume there is a difference between primitives and Classes - isn't the return value of Complex foo() an rvalue? Thanks!

Aucun commentaire:

Enregistrer un commentaire