如何将 const Uint8* 转换为 Uint8*

how to convert const Uint8* to Uint8*

我认为这是不言自明的。我需要能够将 const Uint8 * 转换为 Uint8 *。我试过简单地将 const Uint8 * 放入 const Uint8 * 并使用 static_cast。然而,每次它都给我这个错误:

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

有人知道我怎样才能成功转换吗?

您可以为此使用 const_cast,例如:

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

然而:你确定你真的在做正确的事吗?请确认您在删除常量后没有修改基础值,因为这是未定义的行为。

[ 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]