c++类中公共函数的地址

Address of a public function from a C++ class

本文关键字:函数 地址 c++      更新时间:2023-10-16

我的应用程序需要从C程序调用部分c++代码。
当我必须获得公共函数的地址时,问题就出现了。
我不知道如何从类中获取公共函数的地址。我特别需要地址,以便我可以将地址作为函数指针传递。
我们也欢迎任何其他绕过该解决方案的解决方案。
我试过静态铸造,但它没有工作。

static_cast<izot_events*>(thisizot_events)->myIzotWink()

调用函数,但我对地址感兴趣。
我试过使用

static_cast<izot_events*>(thisizot_events)->myIzotWink

但是这会返回一个错误。
这里还有一些代码供参考。

void* C_Create() { return new izot_events(); }
thisizot_events = C_Create();
static_cast<izot_events*>(thisizot_events)->myIzotWink // This does not work i.e I cant get the value.
static_cast<izot_events*>(thisizot_events)->myIzotWink() // While this gets called

将函数设置为静态,然后可以执行&MyClass::my_function。你不应该做任何铸造;相反,让你的静态函数有正确的声明来匹配函数指针的类型。