We use array of function pointers. Here is sample code
// array of functions
#include
// -- FUNCTION PROTOTYPES --
void func1();
void func2();
void func3();
void func4();
void func5();
// -- ENDS --
void main()
{
// notice the prototype
void (*ptr[5])();
// arrays are made to point
// at the respective functions
ptr[0]=func1;
ptr[1]=func2;
ptr[2]=func3;
ptr[3]=func4;
ptr[4]=func5;
// now the array elements
// point to different functions
// which are called just like
// we access the elements of
// an array
for(int i=0;i<5;i++)
(*ptr[i])();
}
// -- FUNCTIONS DEFINITION --
void func1()
{
cout<<"Called Func1!\n";
}
void func2()
{
cout<<"Called Func2!\n";
}
void func3()
{
cout<<"Called Func3!\n";
}
void func4()
{
cout<<"Called Func4!\n";
}
void func5()
{
cout<<"Called Func5!\n";
}
// -- ENDS --
No comments:
Post a Comment