• 模仿c#Func和Action的函数指针模板


    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 }
  • 相关阅读:
    vue开发环境搭建
    超出宽度省略号,css解决办法
    css平板手机pc媒体查询
    pc和移动端调用qq聊天
    fiddeler 抓包工具的配置使用
    给证件照换背景颜色
    markman高效的设计稿标注、测量工具
    画三角形和直角三角形
    vue中使用Ueditor编辑器 -- 1
    js获取浏览器缩放比例
  • 原文地址:https://www.cnblogs.com/xuybin/p/2796063.html
Copyright © 2020-2023  润新知