GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Tuesday, March 8, 2011

CPP - Pointer To Function

/**
 * Below is a code that shows on how to use function to pointer
 * The return type is void, as well as the argument of the parameter
 */

#include <iostream>

using namespace std;

void displayFunc(void (*func)(void));
void display(void);

void display(){
    cout << "lola is lol" << endl;
}

void displayFunc(void(*func)(void)){
    func();
}

int main()
{
    displayFunc(display);
    return 0;
}

/**
 * While this below is a pointer to function that has argument
 * parameter type of int
 */

#include <iostream>

using namespace std;

void displayFunc(void(*func)(int));
void display(void);

void display(int x){
    cout << "lola is lol" << endl;
    cout << x << endl;               // retrieve the value of x
}
void displayFunc(void(*func)(int)){
    func(5);
}

int main()
{
    displayFunc(display);
    return 0;
}


Share/Bookmark

No comments:

Post a Comment