网站首页 > 技术文章 正文
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;
}
- 上一篇: C++ 之 自增运算符输出26个英文字母
- 下一篇:已经是最后一篇了
猜你喜欢
- 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++ 之“自增运算符”(自增运算符放在变量前面和后面的区别)
- 最近发表
- 标签列表
-
- cmd/c (64)
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- sqlset (64)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)