回调函数就是一个通过函数指针调用的函数。如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数。回调函数不是由该函数的实现方直接调用,而是在特定的事件或条件发生时由另外的一方调用的,用于对该事件或条件进行响应。-百度百科
机制编辑
2意义编辑
代码VC++ MFC(列举所有桌面窗口,窗口标题放在全局变量string buf[]数组中,n是静态变量): BOOL CALLBACK lpEnumFunc(HWND hwnd, LPARAM lParam) { char str[100]; ::GetWindowText(hwnd,str,sizeof(str)); buf[n++]=str; return 1; } void CEnumWindowsDlg::OnOK() { // TODO: Add extra validation here n=0; EnumWindows(lpEnumFunc,NULL); int i; for(i=0;i<n;i++) { if(buf!="") m_list.AddString(buf.c_str()); } //CDialog::OnOK(); }
// callbacke.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "windows.h" #include "iostream" using namespace std; //回调函数指针 typedef int(WINAPI* WNDCALLBACK)(int*,int*); //回调函数 int __stdcall Call(int *a,int *b); //定义类 class A { public: //比较两个数字大小 int Compare(int m,int n,WNDCALLBACK function) { int *a=&m; int *b=&n; return (*function)(a,b); } }; int main(int argc, char* argv[]) { int a=10; int b=6; A m_a; cout<<m_a.Compare(a,b,Call)<<endl; system("pause"); return 0; } //回调函数 int CALLBACK Call(int *a,int *b) { return *a>(*b)?(*a):(*b); }