函数注册与回调实例

来源:互联网 发布:linux安装tomcat8 编辑:程序博客网 时间:2024/05/22 03:26

#include <iostream>

#include <map>

#include <string>


using namespace std;


int print(int a, int b)

{

cout << "Function of Print a: " << a << "  b: " << b << endl;

return a+b;

 }


int main()

{

string codeStr;

cout << "Input Code String: ";

cin >> codeStr;


typedef int (*function)(int, int);

map< string, function > mapProc;

mapProc[codeStr] = print;

string codeTest;

cout << "Input Test Code String: ";

cin >> codeTest;


map<string, function>iterator it = mapProc.find( codeTest );

if (it != mapProc.end())

{

int ret1 = ((*it).second)(1, 3);

cout << "Call Back Function ret1: " << ret1 << endl;

function fp = (*it).second;

int ret2 = fp(4, 7);

cout << "Call Back Function ret2: " << ret2 << endl;

 }

else

{

cout << "CodeTest string not equal  CodeStr....." << endl;

 }

return 0;

}

0 0
原创粉丝点击