Docsity

Function Pointers in C++: Declaration, Initialization, Invocation, and Benefits, Exams of Object Oriented Programming

Toggle sidebar
bg1
Pointers to functions
C++ allows operations with pointers to functions. The typical use of this is for passing a
function as an argument to another function. Pointers to functions are declared with the
same syntax as a regular function declaration, except that the name of the function is
enclosed between parentheses () and an asterisk (*) is inserted before the name:
// pointer to functions
#include <iostream>
using namespace std;
int addition (int a, int b)
{ return (a+b); }
int subtraction (int a, int b)
{ return (a-b); }
int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}
int main ()
{
int m,n;
int (*minus)(int,int) = subtraction;
m = operation (7, 5, addition);
n = operation (20, m, minus);
cout <<n;
return 0;
}
1
2
3
4
5
6
7
void one() { cout << "One\n"; }
void two() { cout << "Two\n"; }
int main()
{
void (*fptr)(); //Declare a function pointer to voids with no params
pf3
pf4

Partial preview of the text

Download Function Pointers in C++: Declaration, Initialization, Invocation, and Benefits and more Exams Object Oriented Programming in PDF only on Docsity!

Pointers to functions

C++ allows operations with pointers to functions. The typical use of this is for passing a

function as an argument to another function. Pointers to functions are declared with the

same syntax as a regular function declaration, except that the name of the function is

enclosed between parentheses () and an asterisk (*) is inserted before the name:

// pointer to functions

#include

using namespace std;

int addition (int a, int b)

{ return (a+b); }

int subtraction (int a, int b)

{ return (a-b); }

int operation (int x, int y, int (*functocall)(int,int))

int g;

g = (*functocall)(x,y);

return (g);

int main ()

int m,n;

int (*minus)(int,int) = subtraction;

m = operation (7, 5, addition);

n = operation (20, m, minus);

cout <<n;

return 0;

void one() { cout << "One \n "; } void two() { cout << "Two \n "; } int main() { void (*fptr)(); //Declare a function pointer to voids with no params

fptr = &one; //fptr - > one *fptr(); //=> one() fptr = &two; //fptr - > two *fptr(); //=> two() return 0 ; }

The above would work as expected, outputting "One" and then "Two". When setting and

accessing the value of function pointers, note that the dereference and "address of"

operators aren't actually necessary - usually they aren't used for the sake of simplicity,

as shown in the modified snippet below:

void one() { cout << "One \n "; } void two() { cout << "Two \n "; } int main() { void (*fptr)(); //Declare a function pointer to voids with no params fptr = one; //fptr - > one fptr(); //=> one() fptr = two; //fptr - > two fptr(); //=> two() return 0 ; }

If a function pointer needs to point to functions that take parameters, the data-types of

these parameters can simply be separated by commas in the function pointer

declaration. Take for example the following:

void one(int a, int b) { cout << a+b << " \n "; } void two(int a, int b) { cout << a*b << " \n "; }

Declaring

Declare a function pointer as though you were declaring a function, except with a

name like *foo instead of just foo:

void (*foo)(int);

Initializing

You can get the address of a function simply by naming it:

void foo();

func_pointer = foo;

or by prefixing the name of the function with an ampersand:

void foo();

func_pointer = &foo;

Invoking

Invoke the function pointed to just as if you were calling a function.

func_pointer( arg1, arg2 );

or you may optionally dereference the function pointer before calling the function it

points to:

(*func_pointer)( arg1, arg2 );

Benefits of Function Pointers

 Function pointers provide a way of passing around instructions for how to do

something

 You can write flexible functions and libraries that allow the programmer to

choose behavior by passing function pointers as arguments

 This flexibility can also be achieved by using classes with virtual functions