《Effective C++》条款10:令operator=等运算符返回reference to *this(连等左值)

该条款主要是为了连锁形式而服务,即形如:

x=y=z=15;

即:

x=(y=(z=15));

 

为了能够实现该功能,常常采用返回赋值符号的左边变量(当然其他操作也可以这样进行);

即如下所示:

class wideget {
public:
	wideget& operator+=(const wideget& rhs) {
		//...do something;
		return *this;
	}
	wideget& operator=(const wideget& rhs) {
		return *this;
	}
};

  

posted @ 2020-12-03 23:45  暮云林凌  阅读(97)  评论(0)    收藏  举报