Converting froma char* to a const char*

Hello all,

Can I safely cast from a char* to a const char* like this:

const char * destPtr = reinterpret_cast<const char *>(srcPtr);

Is this safe or is there some C++ gotcha that I need to be aware of.

Many thanks,

Luca
for this reinterpret_cast is not required. a simple c-style cast will do.


const char * destPtr = (const char *)srcPtr;


infact you dont need any casting. converting char to const char doesnt require any cast. had it been opposite then you need a simple cast.. i.e. from const char* to char*.
Many thanks!
You don't need to cast at all in this instance as it's completely safe.

If is not safe to use a const pointer as a non-const pointer, and so you must override the compiler to allow this use.

Whenever you use a cast, you are overriding the compilers type system. In C++ you should never do this lightly. C++ has four cast methods that force you to think about the cast you're doing. If you use C style casts, you're even subverting that.
+1 kbw. Never use C-style casts, period.
Topic archived. No new replies allowed.