C++ storing function for later

Started by
8 comments, last by King Mir 17 years, 5 months ago
In C++. I've been reading about function pointers. But what i want to know is if it's possible to store a function for later use. Also, how would i store a function without knowing the parameters? Any info is greatly apreciated, thanks.
Advertisement
Quote:Original post by charlando
In C++. I've been reading about function pointers. But what i want to know is if it's possible to store a function for later use. Also, how would i store a function without knowing the parameters?
Any info is greatly apreciated, thanks.

Not that this answers your question, but you might seriously want to consider using a different language. I'm currently using Lua to do exactly what you describe. Lua is also written as a C library, so using it in conjunction with C++ is easy as pie.

Essentially, my C++ code is just rendering routines, and Lua handles all the logic.

EDIT:
To clarify, functions are first-class variables in Lua, and variables are dynamically typed. So, something like this is perfectly acceptable Lua:
--Two dashes are a line commentmyVar = 3 --no need to declare type, the value "3" carries the typefunction Add(firstArg, secondArg)	return (firstArg + secondArg)endmyVar = Addprint(myVar(4, 5)) --prints 9


I also like the way they handle variable arguments:
function DoStuff(...)	--variable arguments are represented by ellipses	print(arg)	--arg is a table holding the arguments	unpack(arg)	--unpack converts arg from a Lua table to a Lua tupleend


[Edited by - templewulf on November 8, 2006 2:33:06 PM]
XBox 360 gamertag: templewulf feel free to add me!
Quote:Original post by charlando
In C++. I've been reading about function pointers. But what i want to know is if it's possible to store a function for later use.
The easiest way by far is to use a typedef:
int SomeFunc(int a, int b);typedef int (*LPFunction)(int a, int b);int main(){   // Create a variable called pfnFunc which stores a pointer to SomeFunc   LPFunction pfnFunc = SomeFunc;   // Call the function via the pointer   int n = pfnFunc(42, 1337);}


Quote:Original post by charlando
Also, how would i store a function without knowing the parameters?
You don't :P
To create a function pointer, you need to know what parameters it takes. The other alternative is to cast the function pointer or use a variable argument function, but both of those ways aren't very safe.

If you're using C++, functors are usually preferable, since they're a bit like OOPy function pointers.
In C++, you cannot deal with functions at all when you don't know their signatures. Also, in C++, you can't store a function, just a pointer to it.

If you want to do stuff like this, templewulf is right, you should prbably switch to a language with better support for functional programming. Python is nice.
One way to mimic unknown number of arguments is to have the function take an array/std::vector of some base class that all arguments are derived from. A simple example is main, which takes an array of c strings. Your function could say take a std::vector<std::string> if that was suitable.

If the arguments to these functions all have some similarity this method could work.
Quote:Original post by rip-off
One way to mimic variable number of arguments is to have the function take an array/std::vector of some base class that all arguments are derived from. A simple example is main, which takes an array of c strings.

If the arguments to these functions all have some similarity this method could work.

That's actually a great idea, but you would then be using arguments polymorphically, rather than a calling object, wouldn't you? It's starting to smell like he might want to re-orient his classes toward this end.

At least, that's what always happens to me when I think I need a heterogenous, variable length list of arguments. It eventually turns out I'm just bad at designing class hierarchies.
XBox 360 gamertag: templewulf feel free to add me!
Quote:Original post by charlando
In C++. I've been reading about function pointers. But what i want to know is if it's possible to store a function for later use.


Of course; that's what the function pointer effectively does.

Quote:Also, how would i store a function without knowing the parameters?


You can't, for good reason. Imagine if it were possible. How would you then "later use" the function, seeing as you don't know what parameters it takes?
Darn it, C++ has a weakness... But i'm so fond of it :/
I think i'll find a different way to do things... typdef might be a way...
As for learning a new language, i don't think SDL works with those languages. I might work with them for other things though :)
Thanks for all the help.
Quote:Original post by charlando
Darn it, C++ has a weakness... But i'm so fond of it :/


I don't think this is a weakness in the language (not that there aren;t any, but this isn;t among them).

You can store a pointer to a function. You can't store a function, because there are many architectures that do not allow executing code in the data segment. this is a security feature of the language.

A function's type includes the parameters, so pointers to functions of different type are not interchangable. This is called type safety and prevents the sorts of errors that frequently occur when people don't really know what they're doing (but think they do). This is a positive feature of the language.

You can bind arguments to function invocations (creating a function closure). It's not a part of the language proper, but rather enabled by its standard library and certainly useful as an idiom. To find out more, google for "functors" or "functional objects". This is how you effective store a function for later use in C++. It's how you would, for example, impement the GOF's "command" pattern. Alexandrescu, in his seminal work Modern C++ Design devotes and entire chapter on ways to implement stored functions completely generically.

So, it's not a weakness of the language. Your fondness does not go unwarranted. Don't leave us now....

Stephen M. Webb
Professional Free Software Developer

1)It is actually pretty safe to cast function pointers of different types. It's sometimes used in state machines to circumvent the inability of a function to return it's own type.

2)It is very possible with functors. A functor is an object that behaves like a function pointer. As an object, a functor can be made to be a child to another class. That class can be a parent to any object, including different functors with different types. There for a pointer to the parrent class can act as a generic functor pointer.

Also, functors allow overloading; just like a function (but not a function pointer), the same functor can call a different version of the opperation, based on the types of arguements passed.

3)It is unlikely that you actually need a generic function pointer. Most likely there is a better way to do what you want to do.

This topic is closed to new replies.

Advertisement