Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of function pointer and callback function

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Xiaobian to share with you the example analysis of function pointers and callback functions, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article. Let's learn about it!

one。 Function pointer

With regard to the concept of a function pointer, you can think of an integer pointer pointing to an integer whose value is the address of the object it points to; a string pointer pointing to a string whose value is the first address of the string pointed to; therefore, a function pointer is of course a pointer variable, it points to a function, and its value is the entry address of the function it points to.

Function pointers are defined as follows:

Typedef int data_type;data_type (* pfun) (data_type, data_type)

A function pointer pfun is defined in the above statement, which indicates that it points to a function with a return value of data_type and arguments of type data_type. The parentheses above the first parenthesis (* pfun) cannot be omitted, otherwise it will become:

Data_type * pfun (data_type, data_type)

In this way, you declare a function named pfun, and its return value is that the data_type*, parameter is two parameters of type data_type.

Here is a chestnut to illustrate the use of function pointers:

# include using namespace std;typedef int data_type;data_type add (data_type& a, data_type& b) {return (a + b);} int main () {data_type a = 2; data_type b = 3; data_type (* pfun) (data_type&, data_type&); pfun = & add; cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report