Search code examples
c++sdl

how to convert const Uint8* to Uint8*


I think it is pretty self explanatory. I need to be able to convert a const Uint8 * to a Uint8 *. I have tried simply just put a const Uint8 * into const Uint8 * and using static_cast. however, every time it gave me this error:

error: 
  assigning to 'Uint8 *' (aka 'unsigned char *') from incompatible type
  'const Uint8 *' (aka 'const unsigned char *')
keyboardstate = SDL_GetKeyboardState(NULL);

Anybody know how I can convert it successfully?


Solution

  • You can use const_cast for this, e.g:

    Uint8* foo = const_cast<Uint8*>(bar);
    

    However: Are you sure that you are really doing the correct thing? Please verify that you don't modify the underlying value after removing constness as this is undefined behaviour.

    [ Note: Depending on the type of the object, a write operation through the pointer, lvalue or pointer to data member resulting from a const_­cast that casts away a const-qualifier may produce undefined behavior ([dcl.type.cv]). — end note ] [expr.const.cast.6]