Use of 'const' for function parameters

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

    I use const on function parameters that are references (or pointers) which are only [in] data and will not be modified by the function. Meaning, when the purpose of using a reference is to avoid copying data and not to allow changing the passed parameter.

    Putting const on the boolean b parameter in your example only puts a constraint on the implementation and doesn't contribute for the class's interface (although not changing parameters is usually advised).

    The function signature for

    void foo(int a);
    

    and

    void foo(const int a);
    

    is the same, which explains your .c and .h

    Asaf

提交回复
热议问题