Overloading assignment operator in C++

后端 未结 10 1179
春和景丽
春和景丽 2020-11-30 04:48

As I\'ve understand, when overloading operator=, the return value should should be a non-const reference.


A& A::operator=( const A& )
{
    // check for sel         


        
10条回答
  •  囚心锁ツ
    2020-11-30 05:28

    A good general advice when overloading operators is 'do as primitive types do', and the default behavior of assignment to a primitive type is that.

    Not returning anything could be an option, to disable assignment inside other expressions if you feel the need, but returning a copy does not make sense at all: if the caller wants to make a copy they can make it out of the reference, if they do not need the copy there is no need to generate a temporary that is not needed.

提交回复
热议问题