The const
modifier in C++ before star means that using this pointer the value pointed at cannot be changed, while the pointer itself can be made to point something else. In the below
void justloadme(const int **ptr)
{
*ptr = new int[5];
}
int main()
{
int *ptr = NULL;
justloadme(&ptr);
}
justloadme
function should not be allowed to edit the integer values (if any) pointed by the passed param, while it can edit the int* value (since the const is not after the first star), but still why do I get a compiler error in both GCC and VC++?
GCC: error: invalid conversion from int**
to const int**
VC++: error C2664: 'justloadme' : cannot convert parameter 1 from 'int *' to 'const int *'. Conversion loses qualifiers
Why does it say that the conversion loses qualifiers? Isn't it gaining the const
qualifier? Moreover, isn't it similar to strlen(const char*)
where we pass a non-const char*