• 8.4类成员函数的指针与多态性


    1 类成员函数的指针

    2 类成员函数的指针数组

    3 指向类成员函数的指针的指针

    1 类成员函数的指针

    auto func1 = &com::jia;//C++函数指针

    int (com::*p)(int, int) = &com::jia;//C函数指针

     1 #include <iostream>
     2 
     3 class com
     4 {
     5 private:
     6     int a;
     7     int b;
     8 public:
     9     com(int x, int y) :a(x), b(y)
    10     {
    11 
    12     }
    13     int jia(int a, int b)
    14     {
    15         return a + b;
    16     }
    17     int jian(int a, int b)
    18     {
    19         return a - b;
    20     }
    21     int cheng(int a, int b)
    22     {
    23         return a*b;
    24     }
    25     double chu(int a, int b)
    26     {
    27         return a / b;
    28     }
    29 };
    30 
    31 void main()
    32 {
    33     com com1(100, 20);
    34 
    35     auto func1 = &com::jia;//C++函数指针
    36     int (com::*p)(int, int) = &com::jia;//C函数指针
    37     
    38     std::cout << typeid(func1).name() << std::endl;//打印数据类型,上下一样
    39     std::cout << typeid(p).name() << std::endl;//打印数据类型,上下一样
    40 
    41     system("pause");
    42 }

    2 类成员函数的指针数组

    typedef int(com::*P)(int, int);

    P func1[4] = { &com::jia ,&com::jian,&com::cheng,&com::chu };//使用typedef

    int(com::*func2[4])(int, int) = { &com::jia ,&com::jian,&com::cheng,&com::chu };//不使用typedef

     1 #include <iostream>
     2 
     3 class com
     4 {
     5 private:
     6     int a;
     7     int b;
     8 public:
     9     com(int x, int y) :a(x), b(y)
    10     {
    11 
    12     }
    13     int jia(int a, int b)
    14     {
    15         return a + b;
    16     }
    17     int jian(int a, int b)
    18     {
    19         return a - b;
    20     }
    21     int cheng(int a, int b)
    22     {
    23         return a*b;
    24     }
    25     int chu(int a, int b)
    26     {
    27         return a / b;
    28     }
    29 };
    30 
    31 typedef int(com::*P)(int, int);
    32 
    33 void main()
    34 {
    35     com com1(100, 20);
    36 
    37     P func1[4] = { &com::jia ,&com::jian,&com::cheng,&com::chu };//使用typedef
    38 
    39     int(com::*func2[4])(int, int) = { &com::jia ,&com::jian,&com::cheng,&com::chu };//不使用typedef
    40     
    41     for (int i = 0; i < 4; i++)
    42     {
    43         std::cout << (com1.*func1[i])(10, 20) << std::endl;
    44         std::cout << (com1.*func2[i])(10, 20) << std::endl;
    45         std::cout << std::endl;
    46     }
    47     
    48     system("pause");
    49 }

    3 指向类成员函数的指针的指针

     1 #include <iostream>
     2 
     3 class com
     4 {
     5 private:
     6     int a;
     7     int b;
     8 public:
     9     com(int x, int y) :a(x), b(y)
    10     {
    11 
    12     }
    13     int jia(int a, int b)
    14     {
    15         return a + b;
    16     }
    17     int jian(int a, int b)
    18     {
    19         return a - b;
    20     }
    21     int cheng(int a, int b)
    22     {
    23         return a*b;
    24     }
    25     int chu(int a, int b)
    26     {
    27         return a / b;
    28     }
    29 };
    30 
    31 typedef int(com::*P)(int, int);
    32 
    33 void main()
    34 {
    35     com com1(100, 20);
    36 
    37     P func1[4] = { &com::jia ,&com::jian,&com::cheng,&com::chu };//使用typedef
    38 
    39     int(com::*func2[4])(int, int) = { &com::jia ,&com::jian,&com::cheng,&com::chu };//不使用typedef
    40 
    41     int(com::**funcp)(int, int) = func1;//二级指针,指向类成员函数的指针的指针
    42 
    43     for (; funcp < func1 + 4; funcp++)//指针遍历数组
    44     {
    45         std::cout << (com1.**funcp)(10, 20) << std::endl;
    46     }
    47     
    48     system("pause");
    49 }
  • 相关阅读:
    Domain Logic approaches
    Comparing Spring AOP and AspectJ
    CDI Features
    Java Design Patterns
    第二阶段:代码片段
    第一阶段:学生在线系统需求分析报告
    load data语句实验报告
    Sping AOP Capabilities and Goals
    Java Design Patterns
    CDI Features
  • 原文地址:https://www.cnblogs.com/denggelin/p/5674610.html
Copyright © 2020-2023  润新知