What's the point of const pointers?

后端 未结 17 1913
粉色の甜心
粉色の甜心 2020-11-30 17:28

I\'m not talking about pointers to const values, but const pointers themselves.

I\'m learning C and C++ beyond the very basic stuff and just until today I realized t

17条回答
  •  星月不相逢
    2020-11-30 17:47

    I guess an advantage would be that the compiler can perform more aggressive optimizations inside the function knowing that this pointer cannot change.

    It also avoids eg. passing this pointer to a subfunction which accepts a non-const pointer reference (and could therefore change the pointer like void f(int *&p)), but I agree, that the usefulness is somewhat limited in this case.

提交回复
热议问题