Hey Guys/Gal's

I'm trying to figure out how to make an array of function pointers, but let me explain what i mean:

The array will need a 'key' value (string | char*) and a 'value' (function pointer). So basically the array holds a list of short names of functions. I need to be able to call the functions by their short name, somehow like

someArray["Print"](); // I understand this is probably not correct

And that will call what ever function the value at 'Print' is pointing too. However, each function pointer will be different..

So for example i can somehow do:

void hello(void){ cout << "hello world\n"; }

void (*myPointer)(void) = NULL;
myPointer = &hello;

// then

myArray["hello"] = myPointer; // etc...

I hope you understand what I mean. Thank you for your help!

Cheers, Tristan.

It looks to me like what you need is an STL map

Yea, I tried a map, and when I tried iter->second(); it said something along the lines that 'second' cannot be run as a function... but maybe I am doing it incorrectly?

Try this

#include<iostream>
#include<map>

using namespace std;

typedef void (*pt2Function)(void);

void hello(void){ cout << "hello world\n"; }

int main()
{
	void (*myPointer)(void) = NULL;
	myPointer = &hello;
// then
	
	map <char *,pt2Function> tempMap;
	tempMap["hello"] = myPointer;
	.
	.
	.
	
	return 0;
}

you can access map using itterator;
like

for(map<int,pt2Function >::iterator iter = tempMap.begin() ; iter != tempMap.end() ; iter++)
{
(*iter).first will give u your char * which is function name
(*iter).second is ur function pointer
}

look for map syntax in net
first and second is not a function.

Thanks kunal kislay. But that's pretty much what I did when I tried the using the STL map, my problem is still running that function? Which is where i got confused with the iter->second();

second can be used as a function if it is declared as a function pointer.

//For a map declared like this
    map<string, void(*)(void)> funTable;    // Actually not that much fun

// You can call the function through the iterator in any of these 3 ways
    iter->second();
    (iter->second)();
    (*iter->second)();

do this it will work...

#include<iostream>
#include<map>

using namespace std;

typedef void (*pt2Function)(void);

void hello(void){ cout << "hello world\n"; }

int main()
{
	void (*myPointer)(void) = NULL;
	myPointer = &hello;
// then
	
	map <char *,pt2Function> tempMap;
	tempMap["hello"] = myPointer;



// after all your assignments take them one by one in ur function pointer which ever u want and call

               myPointer = tempMap["hello"];
               myPointer();
	.
	.
	.
	
	return 0;
}

Thanks for all your help people :)

So i've got this little example working:

#include <iostream>
#include <map>
#include <string>

using namespace std;

void printHello(void)
{
	cout << "Hello World" << endl;
}

void printGoodbye(void)
{
	cout << "Goodbye!" << endl;
}

int main(void)
{
	map<string, void(*)(void)> HTable;

	void(*hello)(void) = &printHello;
	void(*goodbye)(void) = &printGoodbye;

	HTable["hello"] = hello;
	HTable["goodbye"] = goodbye;

	map<string, void(*)(void)>::const_iterator iter;

	for(iter = HTable.begin(); iter != HTable.end(); iter++)
		(iter->second)();

	cin.get();

	return 0;
}

Now my next question if you don't mind, seeing as this suits functions of the temple functionName(void);, what if the functions being added had different amounts of parameters or no parameters (like the void one in the example)...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.