对不起,如果这是一个愚蠢的问题,但我无法理解以下代码:
struct myStruct
{
static void func1(const event, void* pthis)
{
myStruct& foo = *static_cast<myStruct*>(pthis);
if(event.action != ...).... return;
if(event.action == ...) foo.func2();
}
void func2()
{}
}
所以... pthis 被强制转换为 myStruct 类型的静态指针?这是否意味着 foo 是“对 myStruct 的引用”类型,并且等于 pthis 指向的值。
本质上 foo 是指向 myStruct,而不访问 myStruct 的实例?
我真的不明白...
最佳答案
So... pthis is casted to a static pointer of type myStruct?
A static_cast
与static
无关变量。在 Regular cast vs. static_cast vs. dynamic_cast 中进行了解释static_cast
可用于反转到 void 指针的隐式转换。
Essentially foo is pointing to myStruct, without access to instances of myStruct?
不,foo
没有“指向”任何东西。 foo
是一个绑定(bind)到被 pThis
取消引用的对象的引用。 foo
可以像 (*pThis)
一样调用成员方法。
关于c++ - 使用 static_cast<> 对指针进行类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25621975/