网站首页 > 技术文章 正文
1、函数指针
函数指针:即可以指向函数地址的指针,经常被用作函数参数,作为回调函数使用。
既然是函数指针,那么肯定与普通函数有关联的,即返回值以及函数的参数列表与普通函数一致。
假设我们构造一个a+b返回c的一个函数,我们对此构造一个指向他的函数指针。
int sum(int a,int b)
{
return a + b;
}
// 指向sum的函数指针
int (*pfunc)(int, int) = ∑为了方便起见,我们会将函数指针构造成一个函数指针的类型。
// 构造一个类型为pfunc_ptr的函数指针类型
typedef int (*pfunc_ptr)(int, int);
// 指向一个函数
pfunc_ptr ptr = ∑ // 不用&也是可以的测试例子
#include "stdfax.h"
/*
global function
*/
int sum(int a,int b)
{
return a + b;
}
int pro(int a, int b)
{
return a * b;
}
double half(double d)
{
return d / 2.0;
}
double twice(double d)
{
return d * 2.0;
}
extern void start_test();
void test_global_func()
{
//int sum(int, int);
//int pro(int, int);
int (*pfunc)(int, int) = ∑
pfunc = &pro; // this is the proper way of function assignment
pfunc = pro; // this is allowed
//double half(double);
//double twice(double);
// dfunc is called function pointer variable
double (*dfunc)(double) = 1/2
dfunc = twice;
cout << dfunc(10) << endl;
typedef int intg_t;
intg_t a = 5;
double(*pd)(double);
pd = 1/2
typedef double (*pdptr_t)(double);
pdptr_t pdd = &twice;
pdd = half;
}
int main()
{
int (*pfunc_ptr)(int a, int b);
pfunc_ptr = ∑
int a = pfunc_ptr(1, 2);
std::cout << "a = " << a << endl;
pfunc_ptr = &pro;
int d = pfunc_ptr(3, 4);
std::cout << "d = " << d << std::endl;
start_test();
return 0;
}2、成员函数指针
成员函数指针与函数指针有一些区别,函数指针构造的函数是全局区域的,而成员函数指针构造的函数是属于类作用域的。
我们可以仿照函数指针来构建成员函数指针。
int (Vector::*pmemfunc_ptr)(int , int );
pmemfunc_ptr = &Vector::sum; // 必须使用取地址符
// 调用的时候必须使用类成员变量来调用
Vector vec;
int ret = (vec.*pmemfunc_ptr)(3,4); // 调用成员函数指针所指向的函数sum测试例子
#include "stdfax.h"
class Vector
{
public:
int sum(int a, int b);
int pro(int a, int b);
};
// Vector::sum means that "sum belongs to vector"
int Vector::sum(int a, int b)
{
return a + b;
}
int Vector::pro(int a, int b)
{
return a * b;
}
void start_test()
{
// member function pointer valiable
int (Vector::*pmemfunc_ptr)(int a, int b);
pmemfunc_ptr = &Vector::sum;
Vector v;
int a = (v.*pmemfunc_ptr)(1, 2);
cout << "a = " << a << endl;
pmemfunc_ptr = &Vector::pro;
int d = (v.*pmemfunc_ptr)(3, 4);
cout << "d = " << d << endl;
}
猜你喜欢
- 2025-09-03 C++11新特性总结 (二)_c++20新特性
- 2025-09-03 EasyC++19,C++中的for循环_c++中foreach循环的用法
- 2025-09-03 C++ 20 新特性(2):支持位段类型的类成员的直接初始化
- 2025-09-03 C++第二课常见的数据类型及变量的声明、变量的赋值及变量的运算
- 2025-09-03 C++ 值简述_c++数值
- 2025-06-10 C++ 之 自增运算符输出26个英文字母
- 2025-06-10 深入了解C++如何注释以及在哪儿注释-开课吧广场
- 2025-06-10 C++中重载运算符的使用(c++重载函数怎么写)
- 2025-06-10 聊聊C++知识点中的for语法及应用(c++里的for语句)
- 2025-06-10 C++函数指针(c++函数指针名称可以省略吗)
- 最近发表
-
- 聊一下 gRPC 的 C++ 异步编程_grpc 异步流模式
- [原创首发]安全日志管理中心实战(3)——开源NIDS之suricata部署
- 超详细手把手搭建在ubuntu系统的FFmpeg环境
- Nginx运维之路(Docker多段构建新版本并增加第三方模
- 92.1K小星星,一款开源免费的远程桌面,让你告别付费远程控制!
- Go 人脸识别教程_piwigo人脸识别
- 安卓手机安装Termux——搭建移动服务器
- ubuntu 安装开发环境(c/c++ 15)_ubuntu安装c++编译器
- Rust开发环境搭建指南:从安装到镜像配置的零坑实践
- Windows系统安装VirtualBox构造本地Linux开发环境
- 标签列表
-
- cmd/c (90)
- c++中::是什么意思 (84)
- 标签用于 (71)
- 主键只能有一个吗 (77)
- c#console.writeline不显示 (95)
- pythoncase语句 (88)
- es6includes (74)
- sqlset (76)
- apt-getinstall-y (100)
- node_modules怎么生成 (87)
- chromepost (71)
- flexdirection (73)
- c++int转char (80)
- mysqlany_value (79)
- static函数和普通函数 (84)
- el-date-picker开始日期早于结束日期 (76)
- js判断是否是json字符串 (75)
- c语言min函数头文件 (77)
- asynccallback (87)
- localstorage.removeitem (77)
- vector线程安全吗 (73)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- 无效的列索引 (74)
