详解 指针函数,函数指针,函数指针函数

在一些STL源码或者linux源码经常能看到这些东西,确实挺让人头疼

指针函数

返回值为指针的函数

复制代码
monney* get_monney(people* p) //传people指针进去 return的是这个人的财产指针
{
   return p->monney_;
}

函数指针

函数指针是一个指向函数的指针,允许通过它调用函数。

复制代码
#include<iostream>
using namespace std;
void text()
{
  cout<<"hello"<<endl;
}
void (*func)();       //声明了一个返回值为void参数为空的函数指针 func     
int main()
{
   
   func=text;              //text初始化func
   //void (*func)()=text;  //可以直接声明并且初始化
   func();                 //打印hello
   return 0;
}

指针函数指针

一个指向指针函数的指针

实质就是一个函数指针,其实没有指针函数指针这种说法

cpp 复制代码
#include <iostream>
using namespace std;

int *get(int &a) {
	return &a;
}

int main() {
	int a = 10;
	int *(*func)(int &) = get;        //声明一个返回值为int*参数为int的函数指针
	cout << func(a) << endl;
	return 0;
}

函数指针函数

返回值为函数指针的函数

cpp 复制代码
#include <iostream>
using namespace std;

void text() {
	cout << "hello" << endl;
}

void (*get_func())() {     //定义了一个函数get_func() 返回一个返回值为void 参数为空的函数指针
	return text;
}
int main() {
	void (*func)() = get_func();
	func();
	return 0;
}

检验一下

static void (*set_malloc_handler(void (*f)()))()

这是一个返回 返回值为void参数为空的函数指针 的set_malloc_handler函数,同时这个函数需要一个函数指针f为参数

ok如果你理解了,你可以继续翻阅你的源码了!

相关推荐
逆小舟2 小时前
【Linux】人事档案——用户及组管理
linux·c++
再见晴天*_*3 小时前
SpringBoot 中单独一个类中运行main方法报错:找不到或无法加载主类
java·开发语言·intellij idea
lqjun08274 小时前
Qt程序单独运行报错问题
开发语言·qt
hdsoft_huge6 小时前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot
风中的微尘6 小时前
39.网络流入门
开发语言·网络·c++·算法
未来之窗软件服务7 小时前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
混分巨兽龙某某7 小时前
基于Qt Creator的Serial Port串口调试助手项目(代码开源)
c++·qt creator·串口助手·serial port
西红柿维生素7 小时前
JVM相关总结
java·jvm·算法
小冯记录编程7 小时前
C++指针陷阱:高效背后的致命危险
开发语言·c++·visual studio
1uther7 小时前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎