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 }

 

转载于:https://www.cnblogs.com/denggelin/p/5674610.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值