I've created an array of pointer to functions, I want to know Is it possible to create array of pointer dynamically, as you see in below I want to change array length dynamically which currently is 2.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
void func1(int);
void func2(int);
int main()
{
void (*func[2])(int) = { &func1, &func2 };
func[0](10);
func[1](20);
cin.ignore();
return 0;
}
void func1(int n)
{
cout << "In func1()\n\tThe value is: " << n << endl;
}
void func2(int n)
{
cout << "In func2()\n\tThe value is: " << n << endl;
}
Make a typedef for the function type:
typedef void (*FunctionType)(int);
Then make a normal dynamic array:
FunctionType* func = new FunctionType[2];
Then you can assign:
func[0] = &func1;
And call:
func[0](1);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments