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 that pointers are passed by value to functions, which makes sense.
This means that inside a function I can make the copied pointer point to some other value without affecting the original pointer from the caller.
So what's the point of having a function header that says:
void foo(int* const ptr);
Inside such a function you cannot make ptr point to something else because it's const and you don't want it to be modified, but a function like this:
void foo(int* ptr);
Does the work just as well! because the pointer is copied anyways and the pointer in the caller is not affected even if you modify the copy. So what's the advantage of const?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…