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:


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 (1,2); This gets the minimum value



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

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.