回调函数

it2022-05-05  125

#include <iostream> using namespace std; typedef void (*CALLBACK)( int a, int b ); class CBase { public: CBase(); public: /* ** 注册回调函数 ** */ void RegisterCallback( CALLBACK fun, int k, int j ); /* ** 调用回调函数 ** */ void CallCallback(); private: /* ** 成员变量: ** 分别保存回调函数指针 ** 回调函数中的形参参数 ** */ int m; int n; CALLBACK func; }; CBase::CBase() { func = NULL; } void CBase::RegisterCallback( CALLBACK fun, int k, int j ) { func = fun; m = k; n = j; return; } void CBase::CallCallback() { func( m, n ); return; } // // 函数1 void fun1( int a, int b ) { cout << "fun1 is called" << endl; cout << a << " " << b << endl; cout << "*****************" << endl; return; } // class CCall { public: /* ** 函数2,类中的函数 ** 注意这是个静态函数 */ static void fun2( int a, int b ); }; void CCall::fun2( int a, int b ) { cout << "fun2 is called" << endl; cout << a << " " << b << endl; cout << "*****************" << endl; return; } // void main(void) { CBase bbbbb; bbbbb.RegisterCallback( fun1, 1, 2 ); bbbbb.CallCallback(); bbbbb.RegisterCallback( CCall::fun2, 10, 20 ); bbbbb.CallCallback(); }

转载于:https://www.cnblogs.com/c007136/archive/2012/05/28/2521594.html

相关资源:DirectX修复工具V4.0增强版

最新回复(0)