• c++ 函数指针


    声明

    int (*pf)( const string &, const string & ); // 正确
    
    • 1

    如上,声明了一个函数指针,注意的是,解引用操作符* 应与返回类型关联所以在这种情况下是与类型名int 关联而不是pf 要想让解引用操作符与pf 关联,所以括号是必需的

    初始化、赋值和调用

    • 函数指针可以用0 来初始化或赋值以表示该指针不指向任何函数
    • 直接去想要调用的函数名,如add,注意,这里没有括号。
    • 将取地址操作符作用在函数名上也能产生指向该函数类型的指针
    #include 
    int lexicoCompare( const string &s1, const string &s2 ) {
    	return s1.compare(s2);
    }
    
    int (*pfi)( const string &, const string & ) = lexicoCompare;//初始化
    pfi = lexicoCompare;//赋值
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    注意问题

    在指向函数类型的指针之间不存在隐式类型转。也就是,只有当赋值操作符左边指针的参数表和返回类型与右边函数或指针的参数表和返回类型完全匹配时初始化和赋值才是正确的如果不匹配则将产生编译错误消息。

    demo

    #include
     
    using namespace std;
    void estimate(int lines, double (*pf)(int));
    double rick(int lines);
     
    int main(void) {
    	int code;
    	cout << "Enter the money:";
    	cin >> code;
    	estimate(code, rick/*计算lines时间的函数*/);
    	return 0;
    }
     
    void estimate(int lines, double (*pf)(int)/*指向执行函数的地址*/) {
    	cout << "Lines code will take time:" << (*pf)(lines) << endl;
    }
     
    double rick(int lines) {
    	return lines*0.05;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    #include 
    #include
    
    using namespace std;
    
    int func1(int a, int b) {
        return a + b;
    }
    int func2(int a, int b) {
        return a - b;
    }
    int func3(int a, int b) {
        return a * b;
    }
    int func4(int a, int b) {
        return a / b;
    }
    
    void Compute(int a, int b, int(*p)(int, int)) {
        cout << p(a, b) << endl;
    }
    int main() {
        int i = 5, j = 10;
        // decltype,在C++中,作为操作符,用于查询表达式的数据类型。decltype在C++11标准制定时引入,主要是为泛型编程而设计
        decltype(func1) *p1 = func1, *p2 = func2, *p3 = func3, *p4 = func4;
        vector vF = { p1,p2,p3,p4 };
        for (auto p : vF)
        {
            Compute(i, j, p);
        }
        system("pause");
        return 0;
    }
    
    • 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
    • 33

    类成员函数,定义函数指针

    指针变量名 = &类名::成员函数名;

    定义的函数指针变量,指向了一个类中的一个函数。但是,当一个类实例化为多个对象的时候,调用函数指针的时候,就必须指定哪一个对象。

    #include 
    #include 
    #include 
    using namespace std;
    
    class student{
        char name[32];
        char addr[32];
        long long number;
        
        student(char *pn,char *pa,long long n){
            strcpy(name,pn);
            strcpy(addr,pa);
            number = n;
        }
        void print(){
            cout <<"name = "<
    • 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
  • 相关阅读:
    sql语句优化总结-避免全表扫描
    Vue组件化编程
    一文让你彻底搞懂AQS(通俗易懂的AQS)
    Web自动化测试(5)-POM
    DP. 数字三角形模型
    TOP K的时间复杂度分析
    生命在于学习——MSF初体验(一)
    FormData多个文件上传(fileList集合)
    Shell基础语法——流程控制、函数
    idea项目中java类名出现带 j 小红点,如何解决?
  • 原文地址:https://blog.csdn.net/qq_27953479/article/details/133677610