Is object copied to the calling function as returned value a lvalue or rvalue?

132 Views Asked by At
#include <iostream>
using namespace std;
class Myclass{
    public:
    int a=3;
    Myclass& operator=(const Myclass& obj){
    a=obj.a;
    return *this;
}
    Myclass operator+(Myclass& obj){
    Myclass temp;
    temp.a=a+obj.a;
    return temp;
}
};
int main()
{
    Myclass A;
    Myclass B;
    B=A+B;
    cout<<B.a;
    return 0;
}

I read a code from the book. It is originally written for strings but I rewrote it with integer to simplify my question.

My question is : The overloaded operator + function return a Myclass object.I think that this object is a rvalue. Then why can it be passed as an argument of operator= function? It needs a reference of Myclass object, which is a lvalue.

Why can this code run successfully?

0

There are 0 best solutions below