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

在一些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如果你理解了,你可以继续翻阅你的源码了!

相关推荐
*.✧屠苏隐遥(ノ◕ヮ◕)ノ*.✧4 小时前
C++学习:基础知识的掌握
c++·visualstudio
江畔柳前堤4 小时前
大语言模型分布式训练:从并行策略到万卡工程的系统梳理
人工智能·分布式·深度学习·算法·目标检测·机器学习·语言模型
wuyk5554 小时前
第2章:六步换相原理全解+STM32工程实战
c语言·开发语言·stm32·单片机·嵌入式硬件
hey_sml4 小时前
JAVA每日一学---CompletableFuture中thenApply与thenCompose区别详解
java·开发语言
Forever Nore5 小时前
LeetCode 4 寻找两个正序数组的中位数 - 二分
算法·leetcode
萧瑟余晖6 小时前
Java深入解析篇十七之SpringCloud
java·开发语言
ttod_qzstudio7 小时前
Java 常用语法极简通关(五):类与对象——字段、方法、构造器、this 与 static
java·开发语言·python
罗西的思考7 小时前
【OpenClaw具身硬件】MiniClaw 阅读笔记---(1)基础
人工智能·算法·机器学习
蛋先生DX8 小时前
大模型参数存储格式揭秘:BF不是男朋友
深度学习·算法·llm
萧瑟余晖8 小时前
Java深入解析篇十七之Spring Security
java·开发语言·spring