Jump to content
  • Advertisement
Sign in to follow this  
janta

cannot convert from 'int **' to 'const int **'

This topic is 3794 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I folks Every once in a while something I thought I understood weel gets me stumped Here it is:
const int** p = new int*[10];
I understand that the compiler complain about converting const to non-const, but I don't understand why it complains here with the following error;
cannot convert from 'int **' to 'const int **'
I know how to shut it up but I'd like to understand before i move forward.

Share this post


Link to post
Share on other sites
Advertisement
const int ** is a pointer to a pointer to a const int. Therefore you need to allocate an array of pointers to const ints:

const int** p = new const int *[10];

Share this post


Link to post
Share on other sites
Consider:

// Here's a constant integer. It must never be changed.
const int should_not_change;

// Here is an evil pointer.
int *ptr = 0;
int **evil = &ptr;

// Let's assume that this conversion is legal.
const int **good = evil;

// This assignment is legal.
*good = &should_not_change;

// However, this will actually change 'should_not_change'
**evil = 10;


Hence, the conversion is illegal.

Share this post


Link to post
Share on other sites
This can be rewritten like this:
typedef const int  MyType; // Const int
typedef MyType * MyTypePtr; // Pointer to const int

MyTypePtr *p;


Now the obvious way to allocate this is
p = new MyTypePtr[10];

Share this post


Link to post
Share on other sites
Presumably you wanted to create a constant pointer to an array of pointers to integers, i.e. you want p itself to be a constant.
That can be done as so:

int** const p = new int*[10];

Share this post


Link to post
Share on other sites
Allright thanks!
I barely ever play with pointer to pointer...

Let me rephrase this, to see if I understand correctly.

We have: a constant integer i and a pointer to integer, initially NULL.


const int i = 0;
int* p = NULL;


God says: p can not ever point to i (let's forget about const_cast for a while)
Then if (A) :


int** good = &p;
const int** bad = good;


were legal,


*bad = &i;


would legally make p point to i. Hence (A) must be illegal.

Share this post


Link to post
Share on other sites
Quote:
Original post by dmatter
Presumably you wanted to create a constant pointer to an array of pointers to integers, i.e. you want p itself to be a constant.
That can be done as so:

int** const p = new int*[10];



Nope, I really wanted an array of pointers to constant objects. In my situation p itself does not need to be constant so I really need "p = new const int*[10]", I think.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!