Use of 'const' for function parameters

前端 未结 30 2846
借酒劲吻你
借酒劲吻你 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:41

    "const is pointless when the argument is passed by value since you will not be modifying the caller's object."

    Wrong.

    It's about self-documenting your code and your assumptions.

    If your code has many people working on it and your functions are non-trivial then you should mark "const" any and everything that you can. When writing industrial-strength code, you should always assume that your coworkers are psychopaths trying to get you any way they can (especially since it's often yourself in the future).

    Besides, as somebody mentioned earlier, it might help the compiler optimize things a bit (though it's a long shot).

提交回复
热议问题