首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

回调函数怎样调用啊该如何解决

2012-03-31 
回调函数怎样调用啊???在.ccp中定义一个回调:C/C++ codevoid __stdcall Notify(int _iID,int _iState)现

回调函数怎样调用啊???
在.ccp中定义一个回调:

C/C++ code
void __stdcall Notify(int _iID,int _iState);


现在我要在一个普通的函数里面调用它,该怎么做呢?
C/C++ code
int ClassXXX::Test(){   //该如何调用Notify呢?直接调?还是再定义一个调用的函数之类的?}




[解决办法]
把函数指针用作Test函数的参数,把 Notify 这个函数传进去
[解决办法]
typedef void __stdcall (*Func) (int,int);

int ClassXXX::Test(Func func)
{
Func();
}

调用的时候就是:
ClassXXX a;
a.Test(Notify);
[解决办法]
通常回调函数由操作系统或父进程调用。
如果真想自己调用回调函数,可以考虑将回调函数全部或部分功能独立出来写成另一个函数,然后在回调函数和正常流程中分别调用这个函数。
[解决办法]
探讨

引用:
typedef void __stdcall (*Func) (int,int);

int ClassXXX::Test(Func func)
{
Func();
}

调用的时候就是:
ClassXXX a;
a.Test(Notify);


Test(Func func)再这样定义一个Test类???貌似这样会报错啊!
……

[解决办法]
直接调用就是了。
你是想callback函数吧?这类函数是系统区调用的,不需要你手动调用。比如启动一个线程的函数,定时器函数等等
[解决办法]
C/C++ code
#include <iostream>using namespace std;typedef int (__stdcall *Func) (int,int);int Calc(int x, int y, Func func){    int sum;    sum = func(x, y);    return sum;}int _stdcall Add(int a, int b){    return a + b;}int _stdcall Sub(int a, int b){    return a - b;}int main(){    int a, b;    cout<<"Please input the first integer:"<<endl;    cin>>a;    cout<<"Please input the second integer:"<<endl;    cin>>b;    int m, n;    m = Calc(a, b, Add);    n = Calc(a, b ,Sub);    cout<<a<<"+"<<b<<"="<<m<<endl;    cout<<a<<"-"<<b<<"="<<n<<endl;    system("pause");    return 0;} 

热点排行