函数包装器管理外部函数

 1 /* 函数包装器管理外部函数 */
 2 
 3 // 函数包装器,用于管理内嵌函数,外部函数调用
 4 template <typename T,typename F>
 5 T run(T v1,T v2,F f)
 6 {
 7     return f(v1,v2);// 函数传入参数
 8 }
 9 
10 int cheng(int a,int b)
11 {
12     return a*b;
13 }
14 
15 void main()
16 {
17     using std::function// C++ 语法,如果有namespace就不需要std,专门引用一个关键字不需要std
18     using std::cout;
19     using std::endl;
20     using std::cin;
21 
22     int num1 = 19;
23     int num2 = 29;
24     
25     function<int(int,int)> fun4 = cheng;// fun4 是函数指针
26     cout << fun4(num1,num2,fun4) << endl;
27 
28 }

 

posted on 2015-05-31 10:14  Dragon-wuxl  阅读(117)  评论(0)    收藏  举报

导航