0

I Have got this error on CppLint :

Using C-style cast.  Use reinterpret_cast<xmlChar *>(...) instead  [readability/casting] [4]

When i try to cast something like this :

xmlChar* something = (xmlChar*) anOtherThing;

But if i do :

xmlChar* something = reinterpret_cast<xmlChar *>(anOtherThing);

I have this error on build:

error: reinterpret_cast from type ‘const char*’ to type ‘xmlChar*’ casts away constness

Could you help me please ?

Sedji Aka
  • 217
  • 2
  • 10
  • 3
    well, replace `xmlChar*` with `const xmlChar*`. The compiler tells you that you **cast away constness** – Vivick Aug 06 '18 at 13:44
  • 2
    The c-style casts are deprecated because they do too many things and often simultaneously. Normally reinterpret_cast is a straight replacement and you keep the same type. I don't see where xmlChar came from - is that the type of anOtherThing? So I would write `reinterpret_cast(anOtherThing)`. But if you are telling me that _anOtherThing_ is actually a const pointer, then you will also need a _const_cast_ i.e. `reinterpret_cast(const_cast(thing))`, or the other way round: `const_cast(reinterpret_cast(thing));` – Gem Taylor Aug 06 '18 at 13:44
  • I suggest you to read this [great answer](https://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used). It explains everything you need to know about cast in C++ and why you shouldn't use C-cast (does too many thing and can hide UB) – Clonk Aug 06 '18 at 13:47
  • Thanks for your help, it's good :) – Sedji Aka Aug 06 '18 at 14:22

1 Answers1

0

So the solution is to replace xmlChar* with const xmlChar* like Vivick said.

But if we use xmlChar* like me we can use the function xmlChartStrdup() instead reinterpret() and it avoids to change all codes to put const.

Thanks all

Sedji Aka
  • 217
  • 2
  • 10