Storing a function in a pointer

I am creating an inventory system that uses a function for on use. I was wondering if i could store a function or at least a pointer to a function in a vector so i can refer to in through an integer stored in each item.
So is it possible to literally do something like.

typedef int (__stdcall *f_funci)();

void function(void)
{
//Function That Does Something
}

int main()
{
f_funci functionstore;
functionstore=function; //I am stuck here
functionstore();
}
Last edited on
Sorry i figured the answer.


typedef int (__stdcall *f_funci)();

void f(void)
{
std::cout <<"hello\n";
}

void main(void)
{
f_funci z=(f_funci)f;
f();
}
Last edited on
Sorry i figured the answer.


No. You didn't.

In the first snippet, function cannot be stored in function_store because the types do not match. The pointer is a pointer to a function with a __stdcall attribute that takes no arguments and returns an int. The function in question doesn't have the __stdcall attribute and doesn't return a value. The second snippet suffers from the exact same problem(s), except you use a c-style cast to silence the compiler. Whether it works or not under any circumstances is anybody's guess. Note that you didn't actually use the stored value. f() calls the function directly.

main returns type int.

Sorry i had adapted some code from another project and forgot to change a few things. I did modify it wrong.

typedef int (__stdcall *f_funci)();

void f(void)
{
std::cout <<"hello\n";
}

void main(void)
{
f_funci z=(f_funci)f;
z();
}

I am just trying to find a way to use a function by using an id to locate it, i managed to create a system that does work, but my issue now is that i need to pass an argument to it. It works if it has a void parameter.
Last edited on
It works if it has a void parameter.

No, it doesn't work. Or better, it works only if you are lucky.
See cire's answer (even for main returning int).
You should change the line typedef int (__stdcall *f_funci)();
to typedef void (*f_funci)(); and the cast is not needed.

Also, void is not a parameter, but it indicates no parameters. It is a C-ism, in C++ you should use void f() instead.

Also, please use code tags to improve readability. See http://www.cplusplus.com/articles/jEywvCM9/

To solve your issue, the following example might help you:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

typedef void (*f_funci)();
typedef void(*f_funci_with_param)(int);

void f()
{
	std::cout << "hello\n";
}

void f(int num)
{
	std::cout << "hello " << num << "\n";
}

int main()
{
	f_funci z = f;
	z();

	f_funci_with_param z_with_param = f;
	z_with_param(3);

	return 0;
}

Notice I used the same function name for both: the compiler chooses the right f function.
If your compiler cannot do it, you should use different names for the 2 functions.
I know how it will default to the one with the correct parameters. Thank you. I have worked with loading functions from a dll but this is the first time i have worked with this. Thank you. Also, thank you again, it works perfectly now.
Topic archived. No new replies allowed.