Cpp - Pointer Function Pointer

Introduction

A function name is a constant pointer to the function.

You can declare a pointer variable that points to a function and to invoke the function using that pointer.

It allows you to create programs that decide which functions to invoke based on user input.

A pointer to a function must point to a function of the appropriate return type and signature.

The declaration of a function pointer always includes the return type and parentheses indicating the type of the parameters.

Consider this declaration:

long (*funcPtr)(int); 

The funcPtr variable is declared to be a pointer that points to a function which takes an integer parameter and returns a long.

The parentheses around *funcPtr are necessary since the parentheses around int have higher precedence than the indirection operator *.

Without the first parenthesis, this would declare a function that takes an integer and returns a pointer to a long.

Examine these two declarations:

long* func(int); 
long (*funcPtr)(int); 
  • func() is a function taking an integer and returning a pointer to a variable of type long.
  • funcPtr is a pointer to a function taking an integer and returning a variable of type long.

Demo

#include <iostream> 
 
void square(int&, int&); 
void cube(int&, int&); 
void swap(int&, int&); 
void getVals(int&, int&); 
void printVals(int, int); 
 
int main() /*  ww w  .  j  a v  a2s  .  c  om*/
{ 
    void (*pFunc)(int&, int&); 
    bool fQuit = false; 
 
    int valOne = 1, valTwo = 2; 
    int choice; 
    while (fQuit == false) 
    { 
            std::cout << "(0) Quit (1) Change Values " << "(2) Square (3) Cube (4) Swap: "; 
            std::cin >> choice; 
            switch (choice) 
             { 
            case 1: 
                  pFunc = getVals; 
                  break; 
            case 2: 
                  pFunc = square; 
                  break; 
            case 3: 
                  pFunc = cube; 
                  break; 
            case 4: 
                  pFunc = swap; 
                  break; 
            default : 
                  fQuit = true; 
                  break; 
             } 
 
            if (fQuit) 
                  break; 
 
            printVals(valOne, valTwo); 
            pFunc(valOne, valTwo); 
            printVals(valOne, valTwo); 
    } 
     return 0; 
 } 
  
 void printVals(int x, int y) 
 { 
     std::cout << "x: " << x << " y: " << y << "\n"; 
 } 
  
 void square(int &rX, int &rY) 
 { 
     rX *= rX; 
     rY *= rY; 
 } 
  
 void cube(int &rX, int &rY) 
 { 
     int tmp; 
  
     tmp = rX; 
     rX *= rX; 
     rX = rX * tmp; 
  
     tmp = rY; 
     rY *= rY; 
     rY = rY * tmp; 
 } 
  
 void swap(int &rX, int &rY) 
 { 
     int temp; 
     temp = rX; 
     rX = rY; 
     rY = temp; 
 } 
  
 void getVals(int &rValOne, int &rValTwo) 
 { 
     std::cout << "New value for valOne: "; 
     std::cin >> rValOne; 
     std::cout << "New value for valTwo: "; 
     std::cin >> rValTwo; 
 }

Result

Related Topics