returning an L-value in C++ need help

travler

Senior member
Feb 28, 2002
220
0
0
this is in my book and I can figure out what it wants me to do, but it sounds simple

the whole program isnt needed

in main there are objects a, b and c

int main()
{
a = b = c;
}

in the class Date we have

Date Date::eek:perators=(const Date& newdate)
{
day = newdate.day;
month = newdate.month;
year = newdate.yar;

return *this
}

which makes the return vaue an R-value.
Im trying to figure out how to modify the assignment operator so the expression a = b = c returns an l-value.

any sugestions, hints , or explanations are appreciated.

thanks

 

RSMemphis

Golden Member
Oct 6, 2001
1,521
0
0
Apart from the omitted semicolon (I assume just a typo here), this sounds correct to me.
In fact, my book says the same and it has worked for me in the past.

 

PCHPlayer

Golden Member
Oct 9, 2001
1,053
0
0
I believe "Date Date:: operator=(const Date &);" should be "Date & Date:: operator=(const Date &);"
The reference makes it an l-value.
 

travler

Senior member
Feb 28, 2002
220
0
0
I think you misunderstand. the code snipets I gave were correct but they are returning an r-value from the function.

I want to return as an l-value.

from another forum I was told to do this it should be writeen as

Date& Date::eek:perator=(const Date& newdate)

yes the missing semi-colon was a typo haha give me a break ;)

I hate how my code gets made into smily faces.

The realy vexing part of the question is it sounds like it wants me to change associativity which is imposible. I just needed to make it a refrerence apparantly
 

PCHPlayer

Golden Member
Oct 9, 2001
1,053
0
0
When you return a "Date" the compiler essentially creates a temporary "Date" object then uses that in the calling expression. Once the statement has been completely evaluated the temporary object is deleted. So there is no way to make a non-reference return value an l-value.