当前位置:网站首页>Function pointer and callback function
Function pointer and callback function
2022-04-22 22:11:00 【One day's death dispersed】


Function names can be treated as constants .
It holds the memory address of the function , The memory address of the function stores the location where the function starts execution ,
Use the pointer to save the address of the function . A pointer to is called a function .

Through function pointers , It can flexibly call various forms ( parameter list ) identical , But functions with different functions .
This greatly increases the flexibility of the code .
For example, define three functions with the same formal parameters and return values , Calculate the two numbers separately and add them , Subtracting the , Multiply

stay main Function , Define two integer values a,b.
There are two definitions int Parameters , Function pointer whose return value is an integer func_ptr, Point to the three functions defined above .


We call the original function directly , And call the function pointer to get the calculation result , You can find :
1) func_ptr The stored value is equal to the address in the function name .
2) The result of calling the function directly , The same result as calling a function through a function pointer .
Like other types of variables , Function pointers can also be passed to functions as function parameters .

The function name itself is the function pointer , Can be passed as an argument to compute_func function .


The result is a normal call .

take sum,difference,product Assign as a parameter to compute_func This process is called the registration of callback functions .
compute_func Call in function sum,differenct,product The process is called the call of callback function .
Application scenarios :
There are two roles , They are the developers of a function , And the user of the function .
The normal scenario is that the user directly calls the function developed by the developer , Just get the return result .
However, some logic of functional functions developed by developers depends on the behavior of users , Depending on the user's behavior , Make different logical processes . At this time, you need to use the callback function .
A hole will be reserved for the general function functions that developers will develop ( Function pointer parameter ). When using this function , The user needs to pass a function pointer to the function , The function pointed to by this function pointer is developed by the user , When the function is running , The process of calling the user developed function through the passed function pointer is a callback process .
The function developed by the user , It's called a callback function .

Based on the above theory , We as users , You can create your own callback function . As a parameter , Pass to compute_func.


Generally speaking , The developer and the user are not the same person , And the functions developed by developers will be more complex ,
in addition , Whatever the callback function does , As long as the callback function passed in by the user is consistent with the formal parameters of the function pointer parameters .
版权声明
本文为[One day's death dispersed]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221432497575.html
边栏推荐
- 智能名片小程序名片详情页功能实现关键代码
- 故障分析 | Federated 存储引擎表导致监控线程处于 Opening table 状态
- 博睿数据携手F5共同构建金融科技从代码到用户的全数据链DNA
- 343 leetcode inverts vowels in a string
- Typescripts Promise
- Remote desktop failure resolution
- UI测试有哪些功能问题?
- Generic best practices: go generic designers teach you how to use generics
- Apache configuration multiport
- Continuous delivery - Blue Ocean application
猜你喜欢

引航成长·匠心赋能——YonMaster开发者培训领航计划全面开启

ES6 proxy and object defineProperty

ES6 reflect object

CSV column extract column extraction

BACKBONE,NECK,HEAD

RT thread studio formats the code

项目经理值得一试的思维方式:项目成功方程式

Cisco switch configuration

从思维走向实践,数字化转型 IT 经营的成功路径

With an annual salary of 37W and a team of 12 people, Tencent was rejected...
随机推荐
Kindeditor displays the source code mode by default on the mobile terminal
Selenium keyboard operation
Interview: artificial intelligence job interview - Framework of necessary knowledge for machine learning algorithm engineers in artificial intelligence job hunting
Android uses SQLite kotlin
The dispute between ideal and integrated technology: where is the safer lidar?
Mysql database optimization
Penetration test & Network & CTF interview questions sorting
JMeter accesses the data of SQL Server with comma '' to CSV file
图像预训练模型的起源解说和使用示例
How far is the meta universe from us? That's what the big guys at the Boao Forum said
京东一面:子线程如何获取父线程 ThreadLocal 的值?我蒙了。。。
Use of NPM
Live app source code, with the page scrolling up and down, automatic ceiling
Sending non-protected broadcast
Demo example of SMS platform API interface - node / SMS / xsend
343 leetcode inverts vowels in a string
How to open a web page online XMIND file (mind map file) and modeling file (. Sldprt file and. Sldasm file)
The use of servlets corresponds to the nine built-in objects
《C语言程序设计》(谭浩强第五版) 第9章 用户自己建立数据类型 习题解析与答案
Apache configuration multiport