• c++ 函数指针 指针函数 void型指针


    一、void型指针

    1、void型指针也称为无类型指针,可以把任意类型的指针值赋给它;

    2、但若加void型指针付给其他类型的指针变量时,必须做强制类型转换。

    3、void类型指针主要用途是编写通用的函数。

    二、函数指针(指向函数的指针

    例1:void myFun(int x);是函数声明。void (*funp)(int);是定义一个指向函数的指针。

    注:函数指针——指向函数的指针,指针函数——函数的返回值是指针。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
      #include <iostream>
    using namespace std;
     
    void myFun(int x);                       //这个声明也可写成:void myFun( int );
    void (*funP)(int );                      //定义一个指向函数的指针,也可写void(*funP)(int x),但习惯上一般不这样。
     
    int main(int argc, char* argv[])
    {
        myFun(10);                           //这是直接调用myFun函数。
     
        funP = &myFun;                        //将myFun函数的地址赋给funP变量,也可以用funP = myFun.
        (*funP)(20);                        //这是通过函数指针变量funP来调用myFun函数的。
        funP(30);                           //也可以这样调用函数。
     
        return 0;
    }
     
    void myFun(int x)
    {
        cout << x << endl;
    }

    /*
    输出:

    10
    20
    30
    */

    例2:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
      #include <iostream>
    using namespace std;
     
    void myFun(int x);
    void mySecondFun(int x);
    void (*funP)(int );
     
    int main(int argc, char* argv[])
    {
        funP = myFun;
        funP(10);
     
        funP = mySecondFun;
        funP(20);
        return 0;
    }
     
    void myFun(int x)
    {
        cout << "myFun" << x << endl;
    }
     
    void mySecondFun(int x)
    {
        cout << "mySecondFun" << x <<endl;
    }
    /*
    输出:

    myFun10
    mySecondFun20
    */

    三、void*和函数指针综合

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
      #include <iostream>
    #include <string.h>
    using namespace std;
     
    int myDiffInt(const void* a, const void* b);
    int myDiffChar(const void* a, const void* b);      //在这两个函数的参数可能是不同类型指针的情况下,
                                                       //如果参数不定义为void*,就不能用一个函数指针调用这两个函数
    int (*myFunP)(const void*,const void*);            //指向函数的指针
     
    int main(int argc, char* argv[])
    {
        int a = 3;
        int b = 1;
        char c1 = 'b';
        char c2 = 'a';
     
        myFunP = myDiffInt;
        cout << myFunP(&a,&b) << endl;
     
        myFunP = myDiffChar;
        cout << myFunP(&c1,&c2) << endl;
    }
     
    int myDiffInt(const void* a, const void* b)   
    {
        return *(int*)a - *(int*)b;
    }
     
    int myDiffChar(const void* a, const void* b)
    {
        return strcmp((char*)a,(char*)b);
    }
  • 相关阅读:
    解决IllegalStateException: Can not perform this action after onSaveInstanceState
    Android自定义控件实战——仿淘宝商品浏览界面
    实现类似于QQ空间相册的点击图片放大,再点后缩小回原来位置
    新浪通过API分享 实践
    Android中集成QQ登陆和QQ好友分享及QQ空间分享
    Android 微信分享,分享到朋友圈与分享到好友,以及微信登陆
    interface Impl
    Spring 4 官方文档学习(十一)Web MVC 框架之编码式Servlet容器初始化
    Spring 4 官方文档学习(十一)Web MVC 框架之HTTP caching support
    Spring 4 官方文档学习(十一)Web MVC 框架之约定优于配置
  • 原文地址:https://www.cnblogs.com/helloweworld/p/2683475.html
Copyright © 2020-2023  润新知