xmyjd
V2EX  ›  C++

C++里, operator=的返回值为什么不是 const& ?

  •  
  •   xmyjd · Oct 17, 2019 · 4515 views
    This topic created in 2400 days ago, the information mentioned may be changed or developed.

    这样不能通过编译。 const T& operator(const T& t);

    必须这样才行 T& operator(const T& t);

    难道会有这样的操作吗: T a,b,c; (a=b)=c;

    7 replies    2019-10-17 15:51:39 +08:00
    mooyo
        1
    mooyo  
       Oct 17, 2019
    T d = (a = b);
    d = c;
    across
        2
    across  
       Oct 17, 2019   ❤️ 1
    大声告诉我, = 的意义是什么,const 的意义又是什么?
    wutiantong
        3
    wutiantong  
       Oct 17, 2019
    会有。
    运算符重载的函数声明都是有规范的,不要自己想当然。
    koebehshian
        4
    koebehshian  
       Oct 17, 2019
    赋值运算符,左边的操作数,必须是的左值 https://en.cppreference.com/w/cpp/language/rule_of_three
    zjsxwc
        5
    zjsxwc  
       Oct 17, 2019
    既想赋值修改,又想 const 不得修改,你到底是想修改还是不想修改?
    shfanzie
        6
    shfanzie  
       Oct 17, 2019
    2 楼 已结贴
    ipwx
        7
    ipwx  
       Oct 17, 2019 via Android
    T& t = (a = b); t = c;
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1250 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 17:35 · PVG 01:35 · LAX 10:35 · JFK 13:35
    ♥ Do have faith in what you're doing.