zoukankan      html  css  js  c++  java
  • C++中的函数指针

    函数指针指向的是函数而非对象,和其他指针一样,函数指针指向某种特定类型,函数的类型由它的返回类型和形参类型共同决定,与函数名无关。

      记个概念,暂时没用到,用到再细细地学一下【逃】

      

      

      

      

      

     重载函数的指针

      编译器通过指针类型决定选用哪个函数,指针类型必须与重载函数的某一个精确匹配。

    函数指针形参

      形参可以是指向函数的指针。此时形参看起来是函数类型,实际上却是当成指针使用。

      

    返回指向函数的指针

      #用到再补全,先挖个坑

    写个小程序练练手

      编写函数的声明,令其接受两个int形参并且返回类型也是Int,然后声明一个cector对象,令其元素是指向该函数的指针。

      写四个函数,分别对两个int值执行加减乘除运算,在vector对象中保存指向这些值的指针,调用vector对象中的每个元素并输出其结果

     1 #include <iostream>
     2 #include<vector>
     3 using namespace std;
     4 
     5 int func1(int a, int b)
     6 {
     7     return a + b;
     8 }
     9 int func2(int a, int b)
    10 {
    11     return a - b;
    12 }
    13 int func3(int a, int b)
    14 {
    15     return a * b;
    16 }
    17 int func4(int a, int b)
    18 {
    19     return a / b;
    20 }
    21 
    22 void Compute(int a, int b, int(*p)(int, int))
    23 {
    24     cout << p(a, b) << endl;
    25 }
    26 int main()
    27 {
    28     int i = 5, j = 10;
    29     decltype(func1) *p1 = func1, *p2 = func2, *p3 = func3, *p4 = func4;
    30     vector<decltype(func1)*> vF = { p1,p2,p3,p4 };
    31     for (auto p : vF)
    32     {
    33         Compute(i, j, p);
    34     }
    35     system("pause");
    36     return 0;
    37 }
  • 相关阅读:
    jenkins构建时报错
    linux查看系统信息
    去掉jenkins的首页警告
    zabbix_server 报警
    OSI七层模型
    linux时间格式总结
    linux系统 lsof命令详解
    SharePoint 2010 用户权限和权限级别
    Error occurred in deployment step 'Activate Features': Unable to locate the workflow's association data.
    使用 Response.Write 向页面body中输出指定html
  • 原文地址:https://www.cnblogs.com/xingzhuan/p/10595190.html
Copyright © 2011-2022 走看看