Examples of C + + function pointers and callback functions

Source: Internet
Author: User
Tags define function function examples min

This article mainly introduces C + + function pointers and callback function examples, need friends can refer to the following

1. Function pointer

function pointer is a pointer, just this pointer it is not like a normal pointer to be a variable, at this point it is pointing to a function, that is, it stores the address of a function, if we change its value, let it point to the address from point Funa to Point Funb, Then the function of the pointer changes.

2. Callback function

What is a callback function? The callback function is actually a function called by the function pointer! If you pass a pointer to a function as a parameter to a B function, and then the pointer in B functions through a function, call a function, this is the callback mechanism. The B function is the callback function.

3. Use of function pointers

3.1 Function Pointer Declaration

typedef return Type (* function pointer type name) (functional parameter list);

The code is as follows:

#include

using namespace Std;

typedef void (*fun) (int,int); Defining function pointer types

void min (int a,int b);

void Max (int a,int b);

void min (int a,int b)

{int Minvalue=a

std::cout<< "min value is" <

}

void Max (int a,int b)

{int maxvalue=a>b?a:b;

std::cout<< "Max value is" <

}

int main ()

{Fun pfun=null;//define function pointer variable Pfun

Pfun=min; Both of the assignment methods are supported

pfun=&min;

Pfun (1,2); This gets the minimum value

Pfun=max;

pfun=&max;

Pfun (1,2); Get maximum value here

return 0;

}

Note : Please pay attention to the triple programming Tutorials section for more wonderful articles .

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.