Use of 'const' for function parameters

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

    There's really no reason to make a value-parameter "const" as the function can only modify a copy of the variable anyway.

    The reason to use "const" is if you're passing something bigger (e.g. a struct with lots of members) by reference, in which case it ensures that the function can't modify it; or rather, the compiler will complain if you try to modify it in the conventional way. It prevents it from being accidentally modified.

提交回复
热议问题