C++/SDL 'void*'不是点到对象类型
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你不需要reinterpret_cast - static_cast应该没问题:
你应该避免reinterpret_cast - 它几乎总是特定于实现的,并且可能隐藏问题 - 就像旧式的C强制转换一样。
You don't need a reinterpret_cast - a static_cast should be OK:
You should avoid reinterpret_cast - it is almost always implementation specific, and may hide problems - just like old-style C casts.
将参数指针投射到一个角色:
Cast your param pointer to a Character:
原因是 C++ 是一种强类型语言。要将一种类型更改为另一种类型,您需要先对其进行强制转换:
The reason is that C++ is a strong typed language. To change one type to another, you need to cast it first:
仅供参考,如果您要在函数中调用很多内容,则可以在任何地方保存所有讨厌的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...