func.h
1 #ifndef FUNC_H 2 #define FUNC_H 3 //函数指针模板 4 template<typename T> 5 class Type{ 6 public: 7 typedef void (*Func)(T); 8 }; 9 template<typename T1,typename TResult> 10 class Type1{ 11 public: 12 typedef TResult (*Func)(T1); 13 }; 14 template<typename T1,typename T2,typename TResult> 15 class Type2{ 16 public: 17 typedef TResult (*Func)(T1,T2); 18 }; 19 template<typename T1,typename T2,typename T3,typename TResult> 20 class Type3{ 21 public: 22 typedef TResult (*Func)(T1,T2,T3); 23 }; 24 template<typename T1,typename T2,typename T3,typename T4,typename TResult> 25 class Type4{ 26 public: 27 typedef TResult (*Func)(T1,T2,T3,T4); 28 }; 29 template<typename T1,typename T2,typename T3,typename T4,typename T5,typename TResult> 30 class Type5{ 31 public: 32 typedef TResult (*Func)(T1,T2,T3,T4,T5); 33 }; 34 template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6,typename TResult> 35 class Type6{ 36 public: 37 typedef TResult (*Func)(T1,T2,T3,T4,T5,T6); 38 }; 39 40 template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6,typename T7,typename TResult> 41 class Type7{ 42 public: 43 typedef TResult (*Func)(T1,T2,T3,T4,T5,T6,T7); 44 }; 45 46 template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6,typename T7,typename T8,typename TResult> 47 class Type8{ 48 public: 49 typedef TResult (*Func)(T1,T2,T3,T4,T5,T6,T7,T8); 50 }; 51 52 template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6,typename T7,typename T8,typename T9,typename TResult> 53 class Type9{ 54 public: 55 typedef TResult (*Func)(T1,T2,T3,T4,T5,T6,T7,T8,T9); 56 }; 57 58 template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6,typename T7,typename T8,typename T9,typename T10,typename TResult> 59 class Type10{ 60 public: 61 typedef TResult (*Func)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10); 62 }; 63 64 #endif
main.cpp
1 #include <iostream> 2 #include "func.h" 3 void incr (int& a) 4 { 5 ++a; 6 } 7 int add (int a) 8 { 9 return ++a; 10 } 11 12 void print (int a) 13 { 14 std::cout << a << std::endl; 15 } 16 17 void print1 (Type<int&>::Func fun,int a) 18 { 19 fun(a); 20 std::cout << a<< std::endl; 21 } 22 23 24 int main() 25 { 26 int x = 7; 27 Type1<int,int>::Func fun1=add; 28 int y=fun1(x); 29 Type<int>::Func fun2=print; 30 fun2(y); 31 fun2(x); 32 print1(incr,x); 33 }