网站首页 > 技术文章 正文
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-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++函数指针名称可以省略吗)
- 2025-06-10 C++ 初学阶段-九九乘法口诀表(九九乘法c++语言程序)
- 2025-06-10 C++|一张图助你理解双重循环(双重循环continue)
- 2025-06-10 C/C++编程笔记:获取电脑串口名称列表
- 2025-06-10 C++引用在本质上是什么,它和指针到底有什么区别?
- 2025-06-10 C++ 之“自增运算符”(自增运算符放在变量前面和后面的区别)
- 08-06中等生如何学好初二数学函数篇
- 08-06C#构造函数
- 08-06初中数学:一次函数学习要点和方法
- 08-06仓颉编程语言基础-数据类型—结构类型
- 08-06C++实现委托机制
- 08-06初中VS高中三角函数:从"固定镜头"到"360°全景",数学视野升级
- 08-06一文讲透PLC中Static和Temp变量的区别
- 08-06类三剑客:一招修改所有对象!类方法与静态方法的核心区别!
- 最近发表
- 标签列表
-
- cmd/c (90)
- c++中::是什么意思 (84)
- 标签用于 (71)
- 主键只能有一个吗 (77)
- c#console.writeline不显示 (95)
- pythoncase语句 (88)
- es6includes (74)
- sqlset (76)
- windowsscripthost (69)
- apt-getinstall-y (100)
- node_modules怎么生成 (87)
- chromepost (71)
- flexdirection (73)
- c++int转char (80)
- mysqlany_value (79)
- static函数和普通函数 (84)
- el-date-picker开始日期早于结束日期 (70)
- asynccallback (71)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)