关于重载加法运算符
关于重载加法运算符,求助
当返回的是原始对象的临时副本是,它是 rvalue,编译器不
允许使用 rvalue 调用成员函数
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);
}
但是这个函数返回的是CBox临时副本,那么例如 aCBox = bCBox +cCBox,这样的对象运算,aCBox由一个临时副本赋值,调用
=重载函数,这个怎么理解.
------解决思路----------------------
没这回事儿。
另外,operator= 绑定的是 acbox,是个左值,和返回的右值对象也没关系。
当返回的是原始对象的临时副本是,它是 rvalue,编译器不
允许使用 rvalue 调用成员函数
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);
}
但是这个函数返回的是CBox临时副本,那么例如 aCBox = bCBox +cCBox,这样的对象运算,aCBox由一个临时副本赋值,调用
=重载函数,这个怎么理解.
------解决思路----------------------
编译器不允许使用 rvalue 调用成员函数
没这回事儿。
另外,operator= 绑定的是 acbox,是个左值,和返回的右值对象也没关系。