Quantcast
Channel: Active questions tagged return-value - Stack Overflow
Viewing all articles
Browse latest Browse all 207

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

$
0
0
#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?


Viewing all articles
Browse latest Browse all 207

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>