xmyjd
V2EX  ›  C++

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

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