首页 > 编程语言 >c++函数指针

c++函数指针

时间:2024-05-28 10:24:23浏览次数:24  
标签:return int float c++ printf 函数指针 include

 

c/c++ 函数指针的用法

【目录】

基本定义

c 函数指针使用举例

c++ 函数指针使用举例

函数指针作为函数参数

函数指针作为函数返回值

函数指针数组

typedef 简化函数指针操作


 

c语言函数指针的定义形式:返回类型 (*函数指针名称)(参数类型,参数类型,参数类型,…);

c++函数指针的定义形式:返回类型 (类名称::*函数成员名称)(参数类型,参数类型,参数类型,….);    

以下代码编译环境:codeblocks with gcc in win 7

c语言函数指针使用举例: 

复制代码
#include <stdio.h>
#include <stdlib.h>

int fun1()
{
    printf("this is fun1 call\n");
    return 1;
}

void fun2(int k, char c)
{
    printf("this is fun2 call:%d %c\n", k, c);
}

int main()
{
    int (*pfun1)() = NULL;
    void (*pfun2)(int, char) = NULL;
    int a,b;
    pfun1 = fun1; //第一种赋值方法
    a = pfun1();  //第一种调用方法(推荐)
    printf("%d\n",a);
    b = (*pfun1)();//第二种调用方法
    printf("%d\n",b);
    pfun2 = &fun2;//第二种赋值方法(推荐,因为和其他数据指针赋值方法一致)
    pfun2(1,'a');
    (*pfun2)(2,'b');
    return 0;
}
复制代码

 

c++函数指针使用举例:

复制代码
#include <iostream>
using namespace std;

class test
{
public:
    test()
    {
        cout<<"constructor"<<endl;
    }
    int fun1(int a, char c)
    {
        cout<<"this is fun1 call:"<<a<<" "<<c<<endl;
        return a;
    }
    void fun2(double d)const
    {
        cout<<"this is fun2 call:"<<d<<endl;
    }
    static double fun3(char buf[])
    {
        cout<<"this is fun3 call:"<<buf<<endl;
        return 3.14;
    }
};

int main()
{
    // 类的静态成员函数指针和c的指针的用法相同
    double (*pstatic)(char buf[]) = NULL;//不需要加类名
    pstatic = test::fun3; //可以不加取地址符号
    pstatic("myclaa");
    pstatic = &test::fun3;
    (*pstatic)("xyz");

    //普通成员函数
    int (test::*pfun)(int, char) = NULL; //一定要加类名
    pfun = &test::fun1; //一定要加取地址符号
    test mytest;
    (mytest.*pfun)(1, 'a'); //调用是一定要加类的对象名和*符号

    //const 函数(基本普通成员函数相同)
    void (test::*pconst)(double)const = NULL; //一定要加const
    pconst = &test::fun2;
    test mytest2;
    (mytest2.*pconst)(3.33);

//    //构造函数或者析构函数的指针,貌似不可以,不知道c++标准有没有规定不能有指向这两者的函数指针
//    (test::*pcon)() = NULL;
//    pcon = &test.test;
//    test mytest3;
//    (mytest3.*pcon)();

    return 0;
}
复制代码

 函数指针作为函数参数:

复制代码
#include <stdio.h>
#include <stdlib.h>

void fun(int k, char c)
{
    printf("this is fun2 call:%d %c\n", k, c);
}

void fun1(void (*pfun)(int, char), int a, char c)
{
    pfun(a, c);
}

int main()
{
    fun1(fun, 1, 'a');
    return 0;
}
// c++ 的形式差不多
复制代码

函数指针作为函数返回值:

复制代码
// c 形式
#include <stdio.h>
#include <stdlib.h>

void fun(int k, char c)
{
    printf("this is fun2 call:%d %c\n", k, c);
}

//fun1 函数的参数为double,返回值为函数指针void(*)(int, char)
void (*fun1(double d))(int, char)
{
    printf("%f\n",d);
    return fun;
}

int main()
{
    void (*p)(int, char) = fun1(3.33);
    p(1, 'a');
    return 0;
}
复制代码 复制代码
//c++ 形式
#include <iostream>
using namespace std;

class test
{
public:
    int fun(int a, char c)
    {
        cout<<"this is fun call:"<<a<<" "<<c<<endl;
        return a;
    }
};

class test2
{
    public:
    // test2 的成员函数fun1,参数是double,
    //返回值是test的成员函数指针int(test::*)(int, char)
    int (test::*fun1(double d))(int, char)
    {
        cout<<d<<endl;
        return &test::fun;
    }
};

int main()
{
    test mytest;
    test2 mytest2;
    int (test::*p)(int, char) = mytest2.fun1(3.33);
    (mytest.*p)(1, 'a');
    return 0;
}
复制代码

函数指针数组:

复制代码
#include <stdio.h>
#include <stdlib.h>

float add(float a,float b){return a+b;}
float minu(float a,float b){return a-b;}

int main()
{
    //定义一个函数指针数组,大小为2
    //里面存放float (*)(float, float)类型的指针
    float (*pfunArry[2])(float, float) = {&add, &minu};
    double k = pfunArry[0](3.33,2.22);// 调用
    printf("%f\n", k);
    k = pfunArry[1](3.33,2.22);
    printf("%f\n", k);
    return 0;
}
//c++ 可类比
复制代码

typedef 简化函数指针类型:

复制代码
#include <stdio.h>
#include <stdlib.h>

float add(float a,float b)
{
    printf("%f\n",a+b);
    return a+b;
}
float minu(float a,float b)
{
    printf("%f\n",a-b);
    return a-b;
}

//用pfunType 来表示float(*)(float, float)
typedef float(*pfunType)(float, float);

int main()
{
    pfunType p = &add;//定义函数指针变量
    p(3.33, 2.22);
    pfunType parry[2] = {&add, &minu};//定义函数指针数组
    parry[1](3.33, 2.22);
    //函数指针作为参数可以定义为:void fun(pfunType p)
    //函数指针作为返回值可以定义为:pfunType fun();

    return 0;
}
//c++ 可类比
复制代码

 【版权声明】转载请注明出处  http://www.cnblogs.com/TenosDoIt/p/3164081.html

来自:https://www.cnblogs.com/TenosDoIt/p/3164081.html

=========

 

参考:

https://www.cnblogs.com/TenosDoIt/p/3164081.html

标签:return,int,float,c++,printf,函数指针,include
From: https://www.cnblogs.com/rebrobot/p/18217276

相关文章

  • lambda表达式的用例 c++
    出自:  https://blog.csdn.net/qq_45604814/article/details/132687858一、Lambda表达式概述1.介绍Lambda表达式是C++11标准引入的一种特性,它提供了一种方便的方式来定义匿名函数。Lambda表达式是一种能够捕捉外部变量并使用它们的函数对象。由捕获列表、参数列表、返......
  • 打开编程世界 跟着Mr.狠人一起学C/C++
    打开编程世界跟着Mr.狠人一起学C/C++自我介绍大家好,我是Mr.狠人。我高中就读于墨尔本,学习的方向是会计,因为疫情我回到国内读大学,大学的专业是国贸,可以说我没有任何的计算机基础。但我在海外研究生阶段毅然决然的选择了计算机专业,我本可以选择金融专业,或是更简单的管理专业......
  • C++ ─── string的模拟实现
            本博客将简单实现来模拟实现string类,最主要是实现string类的构造、拷贝构造、赋值运算符重载以及析构函数。    下期我们继续讲解完整版string的模拟实现(将不再会是浅拷贝了)        说明:下述string类没有显式定义其拷贝构造函数与赋值运......
  • A Simple Problem with Integers(C++)
     【题目描述】这是一道模板题。给定数列 a[1],a[2],…,a[n] ,你需要依次进行q 个操作,操作有两类:C、lrx :给定 l,r,x ,对于所有 i∈[l,r] ,将 a[i] 加上 x (换言之,将 a[l],a[l+1],…,a[r] 分别加上 x );Q、lr :给定l,r ,求 ∑ri=la[i] 的值(换言之,求 a[l]+a[l+......
  • c++设计模式-装饰器模式和代理模式
    namespace_nmsp1{//抽象的控件类classControl{public:virtualvoiddraw()=0;//draw方法,用于将自身绘制到屏幕上。public:virtual~Control(){}//做父类时析构函数应该为虚函数};//列表控件类classListCtrl......
  • 【C++】旋转字符串——精准与否,就是屠宰和手术的区别
    ✨题目链接:NC114旋转字符串✨题目描述 字符串旋转:给定两字符串A和B,如果能将A从中间某个位置分割为左右两部分字符串(可以为空串),并将左边的字符串移动到右边字符串后面组成新的字符串可以变为字符串B时返回true。例如:如果A=‘youzan’,B=‘zanyou’,A按‘you’‘zan’......
  • C++系列-operator new和operator delete函数
    ......
  • 图像处理之基于标记的分水岭算法(C++)
    图像处理之基于标记的分水岭算法(C++)文章目录图像处理之基于标记的分水岭算法(C++)前言一、基于标记点的分水岭算法应用1.实现步骤:2.代码实现总结前言传统分水岭算法存在过分割的不足,OpenCV提供了一种改进的分水岭算法,使用一系列预定义标记来引导图像分割的定义方式......
  • c++箭头运算符在类与结构体之间可以做的功能简介
    #include<iostream>//箭头运算符在类与结构体之间可以做的功能以及重载运算符。#include<string>classentity{public: intx;public: voidprint()const{std::cout<<"hello!"<<std::endl;}};classscopedPtr{private: entity*m_Obj;public: sc......
  • 【C++】开源:RabbitMQ安装与配置使用(SimpleAmqpClient)
    ......