Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

const int* const: assigning to a const int*

Posted on 2007-10-09
10
Medium Priority
?
393 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:mrwad99
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 30

Assisted Solution

by:Axter
Axter earned 160 total points
ID: 20040523
>>No compilation error occurs: how come I can assign a "const int* const" to a "const int*" ?

Because you're assigning value.

When assigning value, you can assign a constant value, to a non-constant.
Example:

const int x = 1234;
int z = x;
0
 
LVL 30

Expert Comment

by:Axter
ID: 20040546
What you can not do is assign a different pointer types.

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.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20040575
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Infinity08
ID: 20040579
Oops ... forgot to refersh before posting ... sorry Axter.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 20040647
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??
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 160 total points
ID: 20040726
The first const in "const int* const" refers to the int that the pointer points to.
The second const in "const int* const" refers to the pointer itself.

In this case :

      const int*  p2 = GetInt();

you're assigning the pointer, NOT the value the pointer points to. So, you're assigning a const pointer to a non-const pointer. Or in other words : you're assigning the const value returned by GetInt (which happens to ba a pointer) to the non-const p2.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 20041421
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?
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 180 total points
ID: 20042435
>>> const int* t = new int ( 10 );
>>> int* s = t;

That doesn't compile as you can't assign a pointer to a const int  to a pointer of a non-const int.

>>>> thereby changing the integer pointed to by 't and breaking the contract.

'breaking the contract' can only be made by casting.

Your initial sample is different. Here you had a  const pointer to a const int and assigned it to a non-const pointer to a const int. That is safe as by 'copying' the value of the pointer (address) you neither deny the constness of the int the pointers now are pointing to, nor did you change the value of the const pointer variable you are copying from. The only thing you do is to initialize a non-const pointer variable to a const int with a valid address. The same happens if you say

   const char * psz = "Hello World";

The right side is a literal which of course is const. The psz is non-const what means I can do

         psz = "Bye Bye";

later without problems. But I can't do

    *psz = 'X';

caause psz is a const char* what means the dereferenced pointer is a const which may not be changed.

Regards, Alex
0
 
LVL 19

Author Comment

by:mrwad99
ID: 20042458
>> Your initial sample is different.

Yeah, I was just breaking it down into two parts.

I get it now, thanks for all the help.
0
 
LVL 30

Expert Comment

by:Axter
ID: 20042486
>>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.

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question