[英]Passing C++ class member function as a paremeter to C function
我在 c++ class 中使用 c dll。 成員函數之一應調用以下 function 生成中斷並調用服務例程:
signed short __stdcall SetIrqConditions
(
signed short DevNum,
unsigned short bEnable,
unsigned int dwIrqMask,
void (__stdcall * funcExternalIsr)
(
signed short DevNum,
unsigned int dwIrqStatus
)
);
I am trying to call another member function of the same class as the last parameter of this function.(funcExternalIsr) When I tried to do this, the compiler complained that the function is not static. So I defined the callee function as a static function, but when I do that I cannot access other members of the class.
class myClass
{
public:
int counter;
void func1();
static void __stdcall func2(signed short DevNum, unsigned int Status);
};
void myClass::func1()
{
...
Result = SetIrqConditions(DevNum, TRUE, Mask, func2); --> no error here once func2 is static
}
void myClass::func2(signed short DevNum, unsigned int Status)
{
counter++; --> invalid use of member 'counter' in static member function
}
我嘗試了許多不同的方法並進行了一些研究,但我似乎無法讓它發揮作用,任何正確方向的指針都將不勝感激。
Passing C++ class member function as a paremeter to C function
使用非靜態 function 無法做到這一點,因為 C 沒有指向成員 function 的指針。 C 只有指向函數的指針,不能指向非靜態成員函數。
您可以做的是編寫一個單獨的免費包裝器 function(或 static 成員函數),在其中調用該非靜態成員 function。 例子:
void wrapper_function(demo args)
{
instance.member_function(args);
}
剩下的問題是在哪里獲取 class 實例以在包裝器中調用。 選項通常是:
void*
參數的 C 樣式回調的選項。 這似乎不是這里的情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.