a question in chapter 8
On page 356,
(motto1 = motto2) = motto3;
If the return type is just CMessage, this will not be legal because a temporary copy of the original object is actually returned so it will be an rvalue, and the compiler will not allow a member function call using an rvalue.
However, on page 361,
// Function to add two CBox objects
CBox CBox::operator+(const CBox& aBox) const
{
// New object has larger length and width, and sum of heights
return CBox(m_Length > aBox.m_Length ? m_Length : aBox.m_Length,
m_Width > aBox.m_Width ? m_Width : aBox.m_Width,
m_Height + aBox.m_Height);
}
the function above is legal for the similar operation like,
smallBox + mediumBox + certainBox
The question is,
the explicit overloaded function call for the assignment operation is
(motto1.operator=(motto2)).operator=(motto3);
the explicit overloaded function call for the addition operator is
(smallBox.operator+(mediumBox)).operator+(certainB ox);
Is there any distinction between the two explicit overloaded function, considering they both return a temporary copy if the former's return type is CMessage? I can't see much difference between them. But the former is not legal and the latter is legal.
Thank you!
|