Use of 'const' for function parameters

前端 未结 30 2810
借酒劲吻你
借酒劲吻你 2020-11-22 03:06

How far do you go with const? Do you just make functions const when necessary or do you go the whole hog and use it everywhere? For example, imag

30条回答
  •  情歌与酒
    2020-11-22 03:23

    If you use the ->* or .* operators, it's a must.

    It prevents you from writing something like

    void foo(Bar *p) { if (++p->*member > 0) { ... } }
    

    which I almost did right now, and which probably doesn't do what you intend.

    What I intended to say was

    void foo(Bar *p) { if (++(p->*member) > 0) { ... } }
    

    and if I had put a const in between Bar * and p, the compiler would have told me that.

提交回复
热议问题