Hi all :)

Currently I have a console for my program, which in short words, executes the function named what you write.

The thing is, the functions needs, for several reasons, to be members of a class.

Currently I have the code below. Manager is a class that needs to travel with the command execution.

typedef void (*Command)(std::string arg, Manager *root);
typedef std::map<std::string, Command> MapType;
MapType commands;
MapType::const_iterator commandPos;

commands.insert(std::make_pair("testcommand", testcommand));

void testcommand(std::string arg, Manager *root)
{
/* do something */
}

Then to execute depending on string I'd do this:

std::string execute = "testcommand";
std::string arguments = "helloworld";
commandPos = commands.find(execute);
(*commandPos).second(arguments, m_rootmanager); // m_rootmanager is my pointer to my Manager class

I've tried in various ways to change my code to call a static function of a class, but I get all sorts of errors.

I tried googling, but the results all seemed far away from my current code, I would like not having to do it in a whole other way than I am already.

Help appreciated :D

Happy new year, y'all :P

It is not clear from your post what exactly you wanted to do. Did you want a function pointer to a static class member function? That is not so difficult and in fact is same as non-class functions. However since the code you have posted does not contain any classes, I'm assuming you are trying something else. Can you tell us what the compiler error is that you are getting in this code? Is your code to insert the value into the map within the main or in global scope in your actual code?

Yes, what I want is to make a pointer to a static class.
The code posted was my current code, with no class.

When making a class, and declaring the functions as static members therein, and adding ClassName:: in front of *Command, I got a compiler error, saying that my function does not take 2 arguments, on line 4 (second syntax in post #1).

Well I would suggest you post the code which is giving the error, may be a pruned version with only the required fns and classes so we can exactly see what's happening. That's always much better.
normally declaring a function pointer to static member function does not require the class name to be put before the function pointer. It is declared just like a non-class function pointer, while assigning the address of the function we need the class name.

example: say i have a class 'A' with a static function 'void func(double)'

void (*pf)(double) = &A::func;

Well I would suggest you post the code which is giving the error, may be a pruned version with only the required fns and classes so we can exactly see what's happening. That's always much better.
normally declaring a function pointer to static member function does not require the class name to be put before the function pointer. It is declared just like a non-class function pointer, while assigning the address of the function we need the class name.

example: say i have a class 'A' with a static function 'void func(double)'

void (*pf)(double) = &A::func;

Thanks.

Using your advice I managed to get it working, or almost.

This is my code:

class Foo
{
public:
	static void bar()
	{
		cout << "Foobar!\n";
	}
};
int main()
{

	typedef void (*command)();
	typedef map<string, command> maptype;

	maptype cmdmap;

	cmdmap.insert(make_pair("bar", &Foo::bar));

	cmdmap.find("bar")->second();

}

Output: Foobar!

Then I tried adding my Manager pointer to the argument list like this:

typedef void (*command)(Manager*);

But I cannot compile - command is no longer the "key" (I have no clue what it is called).

The compiler says this:

error C2440: 'initializing' : cannot convert from 'void (__cdecl *)(void)' to 'command '

It works with a string, or aswell the Manager without the pointer-*.

Help is appreciated :)

As earlier, it would have been better if you had posted the code that gave you the error. Somehow the descriptions don't really make the picture very clear !!

From the compiler error that you have posted it seems that probably you changed the function pointer to point to a function that takes a Manager* but did not make the changes to the function parameter list who's address you are assigning to that fn pointer. However, you've mentioned that It works with string and Manager, so that's confusing. If you can post the code which is giving the error, along with the headers included and the compiler error, we might be able to help better. It could be that the error is not where you think it is.

As earlier, it would have been better if you had posted the code that gave you the error. Somehow the descriptions don't really make the picture very clear !!

Well I did include the code...

class Foo
{
public:
	static void bar()
	{
		cout << "Foobar!\n";
	}
};
int main()
{

	//typedef void (*command)();
        typedef void (*command)(Manager*);
	typedef map<string, command> maptype;

	maptype cmdmap;

	cmdmap.insert(make_pair("bar", &Foo::bar));

	cmdmap.find("bar")->second();

}

Well I did include the code...

class Foo
{
public:
	static void bar()
	{
		cout << "Foobar!\n";
	}
};
int main()
{

	//typedef void (*command)();
        typedef void (*command)(Manager*);
	typedef map<string, command> maptype;

	maptype cmdmap;

	cmdmap.insert(make_pair("bar", &Foo::bar));

	cmdmap.find("bar")->second();

}
cmdmap.insert(make_pair("bar", &Foo::bar));

this is the bar function declaration: void bar ();
this is the fn ptr you declared: typedef void (*command)(Manager*);

you cannot assign the address of a function which takes 0 parameters to a fn ptr of command type since it requires a Manager*. change the bar function to accept a Manager*

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.