const pointer
-
I need to refer something on C++ and found this #include using namespace std; int main() { int foo = 4; int bar = 16; // ptr - non-constant pointer, non-constant data // int* ptr = &foo; *ptr = 6; // OK: Data is non-constant, so it can be // changed via ptr ptr = &bar; // The pointer is non-constant, so it may *ptr = 22; // point to other data And change it // ptr_to_const - non-constant pointer, constant data // const int* ptr_to_const = &foo; return 0; } When i read this const int* ptr_to_const = &foo; Non constant pointer to constant data ; But foo isnt declared as constant . Why does the compiler not warn me of this ?
-
I need to refer something on C++ and found this #include using namespace std; int main() { int foo = 4; int bar = 16; // ptr - non-constant pointer, non-constant data // int* ptr = &foo; *ptr = 6; // OK: Data is non-constant, so it can be // changed via ptr ptr = &bar; // The pointer is non-constant, so it may *ptr = 22; // point to other data And change it // ptr_to_const - non-constant pointer, constant data // const int* ptr_to_const = &foo; return 0; } When i read this const int* ptr_to_const = &foo; Non constant pointer to constant data ; But foo isnt declared as constant . Why does the compiler not warn me of this ?
-
I need to refer something on C++ and found this #include using namespace std; int main() { int foo = 4; int bar = 16; // ptr - non-constant pointer, non-constant data // int* ptr = &foo; *ptr = 6; // OK: Data is non-constant, so it can be // changed via ptr ptr = &bar; // The pointer is non-constant, so it may *ptr = 22; // point to other data And change it // ptr_to_const - non-constant pointer, constant data // const int* ptr_to_const = &foo; return 0; } When i read this const int* ptr_to_const = &foo; Non constant pointer to constant data ; But foo isnt declared as constant . Why does the compiler not warn me of this ?
A pointer to constant data needs to be initialized, that's what this line does, and no specification says the "initializer" must be constant. Using pointers to constant data is in essence declaring an intention not to modify that data, nothing more. Sure, if you try to modify it, no warning will be shown - it'll be an Error! The other possible case is declaring a constant pointer. You certainly can initialize it, but not make it point anywhere else afterwards.
-
I need to refer something on C++ and found this #include using namespace std; int main() { int foo = 4; int bar = 16; // ptr - non-constant pointer, non-constant data // int* ptr = &foo; *ptr = 6; // OK: Data is non-constant, so it can be // changed via ptr ptr = &bar; // The pointer is non-constant, so it may *ptr = 22; // point to other data And change it // ptr_to_const - non-constant pointer, constant data // const int* ptr_to_const = &foo; return 0; } When i read this const int* ptr_to_const = &foo; Non constant pointer to constant data ; But foo isnt declared as constant . Why does the compiler not warn me of this ?
Because prt_to_const is a promise that you won't modify the data through ptr_to_const, not that the data it points to cannot change by other means! One good reason to use const is to force other code to not change something. A class might return a ptr_to_const, that points to private internal class data. The class itself might modify the data via some function call latter, but it would be a mistake to let anything other than the class modify that internal data.