Const correctness for value parameters

前端 未结 6 841
野性不改
野性不改 2020-11-28 12:09

I know there are few question about const correctness where it is stated that the declaration of a function and its definition do not need to agree for value parameters. Thi

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 12:33

    If there is const keyword present; it means value of 'i' (which is const type) can not be modified. If value of 'i' is changed inside foo function compiler will throw error: "

    Can not modify const object

    But changing '*i' (i.e. *i = 3;)means you are not changing value of 'i' but value of address pointed by 'i'

    Actually,the const function is appropriate for large objects that should not be altered by function.

提交回复
热议问题