简体   繁体   English

类成员函数地址

[英]Class member function address

Edit How is this in any way related to the supposed post? 编辑这与假定的帖子有什么关系? They are both completely different errors, you guys should really stop trying to farm rep 他们都是完全不同的错误,你们应该真的停止尝试农场代表

So I've been searching around google and stackoverflow but I couldn't find one solution that would help my case. 所以我一直在谷歌和stackoverflow周围搜索,但是我找不到一种可以解决我问题的解决方案。 I have a D3D9Device pointer and I want the EndScene address of that device, how would I approach so? 我有一个D3D9Device指针,我想要该设备的EndScene地址,我将如何处理呢?

DWORD aEndScene = *(DWORD*)(&d3ddev->EndScene);

won't work with the following error 不会出现以下错误

'&': illegal operation on bound member function expression

I think that's wrong because I'm actually trying to get the address of the d3ddev class 我认为这是错误的,因为我实际上是在尝试获取d3ddev类的地址

Member function pointers are not per object, they are per type. 成员函数指针不是每个对象,而是每个类型。 In your example, you could have multiple instances of a IDirect3DDevice9 , all of which would have the same pointer value for their EndScene member function (assuming they aren't different concrete types - but this isn't likely). 在您的示例中,您可能有IDirect3DDevice9多个实例,它们的EndScene成员函数都具有相同的指针值(假设它们不是不同的具体类型-但这不太可能)。

The specific error you are getting is because you are attempting to get the address of a pointer-to-member function from an object, which isn't valid (eg. see '&' illegal operation on bound member function expression error ). 您收到的特定错误是因为您试图从对象获取指向成员函数的指针的地址,该地址是无效的(例如, 对绑定的成员函数表达式错误参见'&'非法操作 )。

It is possible to get the value of a member function using the type, instead of an object pointer. 可以使用类型而不是对象指针来获取成员函数的值。 However, it's extremely ugly: 但是,这非常丑陋:

// Value is stored in 'end_scene':
HRESULT (IDirect3DDevice9::* end_scene)() = &IDirect3DDevice9::EndScene;
// Call the function, with the value of 'end_scene'.
(*d3ddev.*(end_scene))();
// Print the address of the pointer-to-member function:
printf("%p\n", end_scene);

I wouldn't suggest doing this here, because most functions in IDirect3DDevice9 don't have the same prototype. 我不建议在这里这样做,因为IDirect3DDevice9大多数函数没有相同的原型。 In fact, only BeginScene has the same prototype as EndScene , and it's hard to imagine a situation in which the call could be one or the other, since they need to be called in a specific order. 实际上,只有BeginScene具有与EndScene相同的原型,并且很难想象这种情况下调用可以是一个或另一个,因为它们需要按特定的顺序进行调用。 You could make the case about using this for the functions that get/set vertex/pixel shader constants, as they have the same prototypes, but, it's just as easy to store some other external state to determine which function to call, and much more straightforward. 您可以将其用于获取/设置顶点/像素着色器常量的函数,因为它们具有相同的原型,但是,存储其他一些外部状态来确定要调用的函数也很容易,等等直截了当。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM