• 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 }
  • 相关阅读:
    Jenkins发布.Net Core项目到IIS
    2019 SDN上机第2次作业
    2019 SDN上机第1次作业
    第二次结对编程作业
    第3组 团队展示
    第一次结对编程作业
    第一次个人编程作业
    第一次博客作业
    android json解析及简单例子
    详述Google针对Android平板App发布的十大开发准则
  • 原文地址:https://www.cnblogs.com/denggelin/p/5674610.html
Copyright © 2020-2023  润新知