0

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?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Mikah
  • 326
  • 1
  • 11
  • 2
    Use `const_cast`... – KamilCuk Feb 29 '20 at 20:06
  • Why do you want to cast a immutable thing into a mutable thing in the first place? That looks like a bug to me. – Jesper Juhl Feb 29 '20 at 20:08
  • @KamilCuk `const_cast` is dangerous though. If the object was not originally non-const, then casting away const is Undefined Behaviour if you modify the object. – Jesper Juhl Feb 29 '20 at 20:10
  • 1
    @JesperJuhl that is not correct. Only if the object is actually modified it is UB. `const_cast` is particular important when working with libraries which are not const-aware. – Sebastian Hoffmann Feb 29 '20 at 20:12
  • I have to agree after implementing the const_cast solution the problem was fixed. plus the whole event system of my game was fixed with it. so I say that was one hell of a fix as I can now go throughout my day now knowing that my game is working how it should be. – Mikah Feb 29 '20 at 20:17
  • @SebastianHoffmann: The comment you're replying to says "... if you modify the object". Perhaps it was edited after your reply? – Keith Thompson Feb 29 '20 at 20:35
  • @KeithThompson Yes, the if-part was missing in the original comment – Sebastian Hoffmann Feb 29 '20 at 21:02
  • @Mikah `SDL_GetKeyboardState()` returns a `const Uint8*`. Why is your `keyboardstate` variable declared as `Uint8*` instead of `const Uint8*` to begin with? Do you ever make modifications of the data pointed to by `keyboardstate`? You shouldn't be, since the data is read-only, so `keyboardstate` should be a pointer to `const` data. – Remy Lebeau Feb 29 '20 at 21:49
  • @RemyLebeau the keyboard state variable is modified once every frame. so yes – Mikah Feb 29 '20 at 21:51
  • @Mikah the variable itself, not the data it points at, correct? You need to understand the difference between a "non-const pointer to const data" (what `SDL_GetKeyboardState()` returns) vs a "const pointer to (const) data" (what you may be thinking of). They are NOT the same thing. It is valid to change a non-const pointer at will to point at new data, but you can't change the data it points at if the data is const. You can freely re-assign your `keyboardstate` variable as long as it is a no-const pointer, even if it is pointing at const data. The position of `const` in a declaration matters! – Remy Lebeau Feb 29 '20 at 21:55
  • `const Uint8 *` vs `Uint8 * const` vs `const Uint8 * const` are all different types with different restrictions – Sebastian Hoffmann Mar 01 '20 at 11:43

1 Answers1

4

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]

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Sebastian Hoffmann
  • 2,815
  • 1
  • 12
  • 22