C++中函数指针详解及代码分享
2019/7/10 22:39:52
本文主要是介绍C++中函数指针详解及代码分享,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
函数指针
函数存放在内存的代码区域内,它们同样有地址。如果我们有一个int test(int a)的函数,那么,它的地址就是函数的名字,如同数组的名字就是数组的起始地址。
1、函数指针的定义方式:data_types (*func_pointer)( data_types arg1, data_types arg2, ...,data_types argn);
c语言函数指针的定义形式:返回类型 (*函数指针名称)(参数类型,参数类型,参数类型,…);
c++函数指针的定义形式:返回类型 (类名称::*函数成员名称)(参数类型,参数类型,参数类型,….);
例如: int (*fp)(int a); //这里就定义了一个指向函数(这个函数参数仅仅为一个int类型,函数返回值是int类型)的指针fp。
类成员函数指针与普通函数指针不是一码事。前者要用.*与->*运算符来使用,而后者可以用*运算符(称为“解引用”dereference,或称“间址”indirection)。
普通函数指针实际上保存的是函数体的开始地址,因此也称“代码指针”,以区别于C/C++最常用的数据指针。
而类成员函数指针就不仅仅是类成员函数的内存起始地址,还需要能解决因为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; }
函数指针作为函数参数:
#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++ 可类比
总结
以上就是本文关于C++中函数指针详解及代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:C语言实现的学生选课系统代码分享等,有什么问题可以随时留言,小编会及时回复大家的。
这篇关于C++中函数指针详解及代码分享的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-29易优CMS安装常见问题汇总-icode9专业技术文章分享
- 2024-06-28易优新手必读安装教程-icode9专业技术文章分享
- 2024-06-28忘记eyoucms后台密码怎么办?-icode9专业技术文章分享
- 2024-06-26终极指南:Scrum中如何设置需求优先级
- 2024-06-26AI大模型企业应用实战(25)-为Langchain Agent添加记忆功能
- 2024-06-26小白家庭 nas 搭建方案-icode9专业技术文章分享
- 2024-06-23AI大模型企业应用实战(14)-langchain的Embedding
- 2024-06-23AI大模型企业应用实战(15)-langchain核心组件
- 2024-06-23AI大模型企业应用实战(16)-langchain核心组件
- 2024-06-23AI 大模型企业应用实战(06)-初识LangChain