Hi everyone, I'm trying to understand what happens when a function is marked as const in a class. Consider the following simple class, where a function foo is used as a test function.
Since foo() is marked as const, then I know that the compiler puts a const in front of int *elem;, BUT as you can see, the code compiles even if I modify what is pointed to by elem
Question: I'd conclude that the compiler does write
Right, the 'const' here does NOT propagate through to the data that is being pointed to. The pointer itself is const, and it will only be a compiler error to change the pointer itself in a const member function.
Actually, I was expecting constint* a; to be used.
Just one last quick question: is it correct to say that constint* a means that "a is a pointer to a constant integer"?
I know that the above syntax, in practice, implies that I can't change the data pointed to by a, i.e. I can't do something like a[2] = 3;. But this is a bit weird to me, because actually I have an array of constant integers!
Please tell me what is the correct way in your opinion