C/C++中的函数指针的使用与总结
概要:
- 函数指针介绍
- typedef简化函数指针的定义
- 指向函数的指针的初始化和赋值
- 通过指针调用函数
- 函数指针形参
- 返回指向函数的指针
- 指向重载函数的指针
参考《C++ Primer》 第五版
- 函数指针介绍
函数指针是C++中比较灵活而且重要的部分,对于软件的灵活度上有很大的帮助 !
函数指针指向的是函数而非对象,和其他指针一样,函数指针指向某种特定类型,函数的类型由它的返回类型和形参类型共同决定,与函数名无关。
bool lengthCompare(const string &,const string &);
该函数的类型是bool(const string& ,const string&)。想要声明一个指向改函数的指针,只需要用指针特换函数名即可:
bool (*pf)(const string&, const string&);//未初始化
Note: *pf 两端的括号必不可少,如果不写括号,则pf是一个返回值为bool的指针的函数:
//声明一个名为pf的函数,该函数返回bool*
bool *pf(const string &, const string &);
- typedef简化函数指针的定义
现在我们来定义三个函数指针:
bool(*pf1)(const string &, const string &);
bool(*pf2)(const string &, const string &);
bool(*pf3)(const string &, const string &);
有没有发现一个问题,每次定义都需要这么长,有没有好的办法呢,当然是有的,我们可以用到typedef:
typedef bool(*cmpFcn)(const string &, const string &);
//bool(*pf1)(const string &, const string &);
//bool(*pf2)(const string &, const string &);
//bool(*pf3)(const string &, const string &);
cmpFcn pf1;
cmpFcn pf2;
cmpFcn pf3;
- 指向函数的指针的初始化和赋值
函数指针的赋值:
pf = lengthCompare ;
pf = &lengthCompare ;
因为在C/C++里函数名就是地址,所以以上两者都等价,都可以给函数指针赋值。
- 通过指针调用函数
可以直接使用指向函数的指针调用函数,无须提前解引用:
bool b1=pf("hello","goodbye");
bool b2=(*pf)("hello","goodbye");
bool b3=LengthCompare("hello","goodbye");
//三个等价调用
指向不同函数类型的指针之间不存在转换规则。
我们可以为函数指针赋一个 nullptr或者0 的整型常量表达式,表示该指针没有指向任何一个函数。
bool lengthCompare(const string &s1, const string &s2) {
return s1.size() == s2.size();
}
string::size_type sumLength(const string &s1, const string &s2) {
return s1.size() + s2.size();
}
bool cstringCompare(char *s1, char *s2) {
return strlen(s1) == strlen(s2);
}
pf1 = ;
pf2 = lengthCompare;
//pf3 = sumLength;
//pf4 = cstringCompare;
如果函数返回类型不匹配(如上)、或者形参不匹配,也会报错!
- 函数指针形参
虽然不能定义函数类型的形参,但是形参可以是指向函数的指针。此时,形参看起来是函数类型,实际上确实当成指针使用:
#include<iostream>
#include<string>
using namespace std;
typedef bool(*cmpFcn)(const string &, const string &);
//bool(*pf1)(const string &, const string &);
//bool(*pf2)(const string &, const string &);
//bool(*pf3)(const string &, const string &);
bool lengthCompare(const string &s1, const string &s2) {
return s1.size() == s2.size();
}//第三个形参是函数类型,它会自动地转成指向函数类型的指针
void useBigger(const string &s1, const string &s2, bool(*pf)(const string&, const string&)) {
cout << pf(s1, s2) << endl;
} int main()
{
cmpFcn pf = lengthCompare;
useBigger("hi", "func", pf);
system("pause");
return ;
}
- 返回指向函数的指针
和数组类似,虽然不能返回一个函数,但是能返回指向函数类型的指针,然而我们必须把返回类型写成指针形式,编译器不会自动地将函数返回类型当成对应的指针类型处理。
#include<iostream>
#include<string>
#include<vector>
using namespace std; int demo(int *p, int a) {
return ;
} // ff是一个函数,有一个形参x,返回结果是一个函数指针int(*)(int *,int)
int (*ff(int x))(int *, int) {
cout << x << endl;
return demo;
} int main()
{
int a = ;
cout << ff()(&a, a)<< endl;
system("pause");
return ;
}
同样,要想声明一个返回函数类型的指针,最简单的方法是使用类型别名:
typedef int (*PF)(int *, int);
PF是一个函数指针,指向的函数有两个形参。
于是就可以这么写了:
// ff是一个函数,有一个形参x,返回结果是一个函数指针int(*)(int *,int)
//int(*ff(int x))(int *, int) {
// cout << x << endl;
// return demo;
//} PF ff(int x) {
cout << x << endl;
return demo;
}
- 指向重载函数的指针
编译器通过指针类型决定选取那个函数,指针类型必须与重载函数中的一个精确匹配。
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void ff(vector<double> vec) {
cout << "ff(vector<double vec)" << endl;
} void ff(unsigned int x) {
cout << "ff(unsigned int x)" << endl;
} int main()
{
//void(*pf)(int x) = &ff; 报错
void (*pf)(unsigned int x) = &ff;
//double (*pf2)(vector<double>) = &ff; 报错
void (*pf2)(vector<double>) = &ff;
system("pause");
return ;
}
C/C++中的函数指针的使用与总结的更多相关文章
- QT中使用函数指针
想仿命令行,所以定义了一个类,让一个String 对应一个 function,将两者输入list容器. 类中定义了 QString commandStr; void (MainWindow::*com ...
- Delphi中的函数指针判断是否为空
delphi函数指针 只有@@p才代表了函数指针本身的地址 assigned(p) 判断是否为空 或者用 @p=nil 来判断函数指针是不是为空 Delphi中的函数指针实际上就是指针,只是在使用 ...
- C++中使用函数指针 【瓦特芯笔记】
在C++类中使用函数指针. 类型定义: typedef 返回类型(类名::*新类型)(参数表) //类定义 class CA { public: char lcFun(int a) ...
- 1、C语言中的函数指针
一 通常的函数调用 void MyFun(int x); //此处的申明也可写成:void MyFun( int ); int main(int argc, char* argv[]) { MyFun ...
- C语言结构体中的函数指针
这篇文章简单的叙述一下函数指针在结构体中的应用,为后面的一系列文章打下基础 本文地址:http://www.cnblogs.com/archimedes/p/function-pointer-in ...
- Keil C51 中的函数指针和再入函数
函数指针是C语言中几个难点之一.由于8051的C编译器的独特要求,函数指针和再入函数有更多的挑战需要克服.主要由于函数变量的传递.典型的(绝大部分8051芯片)函数变量通过堆栈的入栈和出栈命令来传递. ...
- C++中的函数指针和指针函数
C++中的函数指针和指针函数 数组名一般可以被当成指向数组第一个元素的常量指针,同样的情况,在函数中,函数名可以本当成指向函数的常量指针,假如一个函数已经定义,那么它在计算机中一定有特定的 ...
- C语言中的函数指针
C语言中的函数指针 函数指针的概念: 函数指针是一个指向位于代码段的函数代码的指针. 函数指针的使用: #include<stdio.h> typedef struct (*fun_t ...
- 利用C语言中的函数指针实现c++中的虚函数
C语言中的函数指针 #include<stdio.h> int fun1(int a) { return a*a; } int fun2(int a) { return a*a*a; } ...
- C/C++中的函数指针
C/C++中的函数指针 一.引子 今天无聊刷了leetcode上的一道题,如下: Median is the middle value in an ordered integer list. If t ...
随机推荐
- JavaScript获取元素尺寸和大小操作总结(转载)
一.获取元素的行内样式 var obj = document.getElementById("test"); alert(obj.height + "\n" + ...
- python--基本数据 类型
数据就是我们变量的值:python中变量保存的是内存地址 变量必须先赋值或者声明才能使用!! 1.数值型 整型 int (python3中int就是长整型,与python2中int不同,另外,pyth ...
- 以太坊上发行ERC20代币
ERC20 代币生成 环境 虚拟主机: ubuntu 18虚拟机 宿主主机: win10; ip:192.168.0.160 1.部署以太坊 1.1 安装GO 安装go,并编译geth 将下载好的go ...
- AI五子棋需求规格说明书
AI-Gobang AI五子棋小程序 github地址:https://github.com/holidaysss/AI-Gobang 程序简介 AlphaGo Zero在世界舞台上取得的巨大成功体现 ...
- redis介绍、安装、redis持久化、redis数据类型
1.redis介绍 2.安装管网:https://redis.io/下载:wget -c http://download.redis.io/releases/redis-4.0.11.tar.gz解 ...
- shell中的函数、数组、报警系统脚本
1.shell中的函数 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这 个小单元的名字即可.格式: function f_name() {commond} ...
- MySQL Sending data导致查询很慢的问题详细分析【转载】
转自http://blog.csdn.net/yunhua_lee/article/details/8573621 [问题现象] 使用sphinx支持倒排索引,但sphinx从mysql查询源数据的时 ...
- Windows 上安装 Azure PowerShell及Azure PowerShell部署虚拟机
一.Azure PowerShell部署 1.使用 PowerShellGet 在 Windows 上安装 Azure PowerShell 从 Azure PowerShell 版本 6.0 开 ...
- inno安装客户端,写注册表url调用客户端
[Registry] Root: HKCR; SubKey: xxx; ValueData: "xxx"; ValueType: string; Flags: CreateValu ...
- 学习笔记DL007:Moore-Penrose伪逆,迹运算,行列式,主成分分析PCA
Moore-Penrose伪逆(pseudoinverse). 非方矩阵,逆矩阵没有定义.矩阵A的左逆B求解线性方程Ax=y.两边左乘左逆B,x=By.可能无法设计唯一映射将A映射到B.矩阵A行数大于 ...