码迷,mamicode.com
首页 > 其他好文 > 详细

链式赋值为何要返回&

时间:2016-07-29 17:13:48      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:

#include<iostream>

using namespace std;

class A{
public:
    A(int x_):x(x_){};
    A/*&*/ operator =(A& h)/*&*/;       //加上后置&就不合法,默认情况他同时支持向左值(&)和右值(&&)赋值
    friend ostream& operator << (ostream& out,const A & h);
private:
    int x;
};
    inline A A::operator =(A& h)
    {
        x = h.x;
        return *this;
    }

    ostream& operator << (ostream& out,const A & h)
    {
        out << h.x;
        return out;
    }
int main(void)
{
    A q(1);
    A w(2);
    A e(3);
    cout << q << " " << w << " " << e << endl;

    //q = w = e;


    (q = w) = e ;   //把值赋给了临时右值
    cout << q << " " << w << " " << e << endl;
    return 0;
}

 

链式赋值为何要返回&

标签:

原文地址:http://www.cnblogs.com/7-29/p/5718908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!