mrwad99
asked on
const int* const: assigning to a const int*
Ah hello.
Consider this code:
const int* const GetInt()
{
const int* const i ( new int ( 10 ) );
return i;
}
int main( int nArgc, TCHAR** lpszArgs )
{
const int* p2 = GetInt();
}
No compilation error occurs: how come I can assign a "const int* const" to a "const int*" ?
TIA
Consider this code:
const int* const GetInt()
{
const int* const i ( new int ( 10 ) );
return i;
}
int main( int nArgc, TCHAR** lpszArgs )
{
const int* p2 = GetInt();
}
No compilation error occurs: how come I can assign a "const int* const" to a "const int*" ?
TIA
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
For the same reason that this works :
const int k = 10;
int l = k;
Remember that the second const in "const int* const" refers to the pointer - so it's a const pointer that is assigned to a non-const pointer, just like a const int can be assigned to a non-const int.
const int k = 10;
int l = k;
Remember that the second const in "const int* const" refers to the pointer - so it's a const pointer that is assigned to a non-const pointer, just like a const int can be assigned to a non-const int.
Oops ... forgot to refersh before posting ... sorry Axter.
ASKER
Thanks both.
>> Because you're assigning value. When assigning value, you can assign a constant value, to a non-constant.
OK. I can see that the simple integer example works, but I cannot see how the second const in
const int* const
is value; surely the first 'const' is the value, the second is the pointer??
>> Because you're assigning value. When assigning value, you can assign a constant value, to a non-constant.
OK. I can see that the simple integer example works, but I cannot see how the second const in
const int* const
is value; surely the first 'const' is the value, the second is the pointer??
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OK. I think I get it.
The const is basically a contract saying "this won't be changed". So in the case of
const int* t = new int ( 10 );
int* s = t;
we are first saying that the integer pointed to be 't' cannot be changed as it is const. However, the second line might invalidate that, as we could dereference what is pointed to by 's', thereby changing the integer pointed to by 't and breaking the contract.
int* const t = new int ( 10 );
int* s = t;
Here we are saying that the pointer value 't' itself wont be changed to point to anything else. Assigning 't' to 's', thereby making 's' point to the same thing as 't' cant possibly result in 't' pointing to something else, so there is no compilation error.
Is this correct?
The const is basically a contract saying "this won't be changed". So in the case of
const int* t = new int ( 10 );
int* s = t;
we are first saying that the integer pointed to be 't' cannot be changed as it is const. However, the second line might invalidate that, as we could dereference what is pointed to by 's', thereby changing the integer pointed to by 't and breaking the contract.
int* const t = new int ( 10 );
int* s = t;
Here we are saying that the pointer value 't' itself wont be changed to point to anything else. Assigning 't' to 's', thereby making 's' point to the same thing as 't' cant possibly result in 't' pointing to something else, so there is no compilation error.
Is this correct?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>> Your initial sample is different.
Yeah, I was just breaking it down into two parts.
I get it now, thanks for all the help.
Yeah, I was just breaking it down into two parts.
I get it now, thanks for all the help.
>>Is this correct?
That's correct.
>>That doesn't compile as you can't assign a pointer to a const int to a pointer of a non-const int.
I believe the questioner is intensionally showing code that can compile, and defining why the compiler doesn't allow it.
That's correct.
>>That doesn't compile as you can't assign a pointer to a const int to a pointer of a non-const int.
I believe the questioner is intensionally showing code that can compile, and defining why the compiler doesn't allow it.
int xyz = 1234;
const int *x = &xyz;
int *z = x; //This will give a compile error, because types don't match
You can't assign z to x, because z is a non constant type, where as x is a constant type.