1

Consider the following code snippet.

int** ptr;  // memory pointed by ptr is initialized to its content.
const int** ptrConst;
ptrConst = ptr // Now I want to convert it type of const int**

It gives compilation error conversion loses qualifier.

Please suggest an alternative to achieve it.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
  • As you can see from the linked dupe this is potentially risky, and in any case you might want to consider moving away from multiple indirection, there's usually better ways of handling this in C++. – user657267 Jan 27 '16 at 06:58
  • The reason why that is now allowed is explained in [Question 11.10 of the C FAQ](http://c-faq.com/ansi/constmismatch.html). Don't do it. – R Sahu Jan 27 '16 at 06:59

1 Answers1

0

You can use const_cast.

ptrConst = const_cast<const int**>(ptr);

You may wish to read this question for information on why this is not a good idea.

Community
  • 1
  • 1
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61