C++/SDL 'void*'不是点到对象类型

发布于 2024-08-16 19:15:06 字数 955 浏览 9 评论 0原文

我是 C++ 新手,我正在尝试使用 C++ 和 SDL 进行一些测试,在 SDL 中我们有一个函数:

SDL_TimerID SDL_AddTimer(Uint32 interval, SDL_NewTimerCallback callback, void *param);

我可以为创建的计时器传递回调。 但显然它将我的实例 this 转换为 *void 所以我无法在静态的更新方法上再次检索它,这很有趣,但 SDL_AddTime 不适用于非静态回调函数。

好吧,所以我的问题是,当尝试通过 void* 参数参数调用公共方法 render 时,它抱怨不是指向对象类型的指针...

有什么方法可以在更新方法,因为我无法控制 SDL_AddTime 函数,并且必须传递所需的参数?

谢谢

#include "Character.h"

Character::Character(void)
{
  timer = SDL_AddTimer(33, update, this);
  this->render(); // is called without problem
}

//static method
Uint32 Character::update(Uint32 interval,void* param)
{
  param->render(); // yields: 'void*' is not a pointer-to-object type;

  SDL_Event event;

  event.type = SDL_USEREVENT;
  event.user.code = 1020;
  event.user.data1 = param;

  SDL_PushEvent(&event);

  return interval;
}

void Character::render(void)
{
  printf("rendering character \n");
}

I'm new on C++ and I'm trying to make some testing with C++ and SDL and in SDL we have a function:

SDL_TimerID SDL_AddTimer(Uint32 interval, SDL_NewTimerCallback callback, void *param);

which I can pass a callback for the timer created.
But apparently it converts my instance this to *void so I can't retrieve it again on the update method which is static, and it's interesting but the the SDL_AddTime doesn't work on a non static callback function.

Well, so my problem is that when trying to call the public method render through the void* param argument It complains about not being a pointer-to-object-type...

Is there any way I can get the Character instance again inside the update method since I don't have control over the SDL_AddTime function and I have to pass the required parameters?

Thanks

#include "Character.h"

Character::Character(void)
{
  timer = SDL_AddTimer(33, update, this);
  this->render(); // is called without problem
}

//static method
Uint32 Character::update(Uint32 interval,void* param)
{
  param->render(); // yields: 'void*' is not a pointer-to-object type;

  SDL_Event event;

  event.type = SDL_USEREVENT;
  event.user.code = 1020;
  event.user.data1 = param;

  SDL_PushEvent(&event);

  return interval;
}

void Character::render(void)
{
  printf("rendering character \n");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱人如己 2024-08-23 19:15:06

你不需要reinterpret_cast - static_cast应该没问题:

Character * cp = static_cast <Character *>( param );

你应该避免reinterpret_cast - 它几乎总是特定于实现的,并且可能隐藏问题 - 就像旧式的C强制转换一样。

You don't need a reinterpret_cast - a static_cast should be OK:

Character * cp = static_cast <Character *>( param );

You should avoid reinterpret_cast - it is almost always implementation specific, and may hide problems - just like old-style C casts.

花开半夏魅人心 2024-08-23 19:15:06

将参数指针投射到一个角色:

Character * charPtr = reinterpret_cast<Character *>(param);
charPtr->render();

Cast your param pointer to a Character:

Character * charPtr = reinterpret_cast<Character *>(param);
charPtr->render();
贩梦商人 2024-08-23 19:15:06

原因是 C++ 是一种强类型语言。要将一种类型更改为另一种类型,您需要先对其进行强制转换:

Uint32 Character::update(Uint32 interval, void* param)
{
    reinterpret_cast<Character* >(param)->render();

    /* ... */
}

The reason is that C++ is a strong typed language. To change one type to another, you need to cast it first:

Uint32 Character::update(Uint32 interval, void* param)
{
    reinterpret_cast<Character* >(param)->render();

    /* ... */
}
╭ゆ眷念 2024-08-23 19:15:06

仅供参考,如果您要在函数中调用很多内容,则可以在任何地方保存所有讨厌的reinterpret_cast内容

Character * myCharacter = reinterpret_cast(param);

然后让你执行“myCharacter->render();”或者你有什么...

Just for reference, if you were to call a lot of stuff in a function, to save all the nasty reinterpret_cast stuff everywhere you can do

Character * myCharacter = reinterpret_cast<Character* >(param);

Which then lets you do 'myCharacter->render();' or whathaveyou...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文