Sign in to follow this  
charlando

C++ storing function for later

Recommended Posts

charlando    122
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.

Share this post


Link to post
Share on other sites
templewulf    526
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 comment

myVar = 3 --no need to declare type, the value "3" carries the type

function Add(firstArg, secondArg)
return (firstArg + secondArg)
end

myVar = Add
print(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 tuple
end


[Edited by - templewulf on November 8, 2006 2:33:06 PM]

Share this post


Link to post
Share on other sites
Evil Steve    2017
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.

Share this post


Link to post
Share on other sites
Simian Man    1022
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.

Share this post


Link to post
Share on other sites
rip-off    10976
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.

Share this post


Link to post
Share on other sites
templewulf    526
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.

Share this post


Link to post
Share on other sites
Zahlman    1682
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?

Share this post


Link to post
Share on other sites
charlando    122
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.

Share this post


Link to post
Share on other sites
Bregma    9199
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....

Share this post


Link to post
Share on other sites
King Mir    2490
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this