Use of 'const' for function parameters

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

    I do not use const for value-passed parametere. The caller does not care whether you modify the parameter or not, it's an implementation detail.

    What is really important is to mark methods as const if they do not modify their instance. Do this as you go, because otherwise you might end up with either lots of const_cast<> or you might find that marking a method const requires changing a lot of code because it calls other methods which should have been marked const.

    I also tend to mark local vars const if I do not need to modify them. I believe it makes the code easier to understand by making it easier to identify the "moving parts".

提交回复
热议问题