如何使回调全局化以便我可以将它用于其他函数?
How to make a callback global so I can use it to other functions?
我这样声明了一个函数:
int __stdcall DoSomething(int &inputSize, int &outputSize, void(* __stdcall progress)(int) )
{
}
如何使 progress() 回调成为一个全局变量,以便在同一个 DLL 的其他函数中使用它?
我是 C++ 的新手。
创建一个具有匹配签名的函数(即 void (*)(int)
)。
#include <iostream>
//void ( * )( int ) - same signature as the function callback
void progressHandler(int progress)
{
std::cout << "received progress: " << progress << std::endl;
}
int DoSomething(int &inputSize, int &outputSize, void (*progress)(int))
{
progress(100);
return 0;
}
int main()
{
int inputSize = 3;
int outputSize = 3;
DoSomething(inputSize, outputSize, progressHandler);
return 0;
}
输出:
received progress: 100
即使我删除了它(因为我使用了 g++
),您也可以保留 __stdcall
.
我这样声明了一个函数:
int __stdcall DoSomething(int &inputSize, int &outputSize, void(* __stdcall progress)(int) )
{
}
如何使 progress() 回调成为一个全局变量,以便在同一个 DLL 的其他函数中使用它? 我是 C++ 的新手。
创建一个具有匹配签名的函数(即 void (*)(int)
)。
#include <iostream>
//void ( * )( int ) - same signature as the function callback
void progressHandler(int progress)
{
std::cout << "received progress: " << progress << std::endl;
}
int DoSomething(int &inputSize, int &outputSize, void (*progress)(int))
{
progress(100);
return 0;
}
int main()
{
int inputSize = 3;
int outputSize = 3;
DoSomething(inputSize, outputSize, progressHandler);
return 0;
}
输出:
received progress: 100
即使我删除了它(因为我使用了 g++
),您也可以保留 __stdcall
.