array of routine pointers

Started by
7 comments, last by Polymorphic OOP 19 years, 4 months ago
Hi. I have a strange question. Is there any way to create a lis or an array of pointers to functions. I mean, i need a list in which each member of the list is a pointer and points to a routine with all the same arguments, but different code. Thanks
Advertisement
Sure.

// C++
#include <boost/function.hpp>typedef boost::function<void (int, int)> sampleFun;void foo(int x, int y) { }void bar(int apples, int oranges) { }sampleFun sampleFunArray[2] = { foo, bar };


// C
typedef void (*sampleFun)(int,int);void foo(int x, int y) { }void bar(int apples, int oranges) { }sampleFun sampleFunArray[2] = { foo, bar };


[edit]
Usage in both cases (C as well as C++): sampleFunArray[index](arg0, arg1);
[/edit]
Quote:Original post by darookie
typedef boost::function<void (int, int)> sampleFun;


O_O How on Earth is the boost::function template declaration done such that you can put *that* in between the angle brackets?
Quote:Original post by Zahlman
Quote:Original post by darookie
typedef boost::function<void (int, int)> sampleFun;


O_O How on Earth is the boost::function template declaration done such that you can put *that* in between the angle brackets?

That's just a regular type parameter. void (int, int) is a function type.
Quote:Original post by Polymorphic OOP
Quote:Original post by Zahlman
Quote:Original post by darookie
typedef boost::function<void (int, int)> sampleFun;


O_O How on Earth is the boost::function template declaration done such that you can put *that* in between the angle brackets?

That's just a regular type parameter. void (int, int) is a function type.
But functions aren't first-class objects in C++, I think. It must be some kind of specialized syntax. Weird.
“[The clergy] believe that any portion of power confided to me, will be exerted in opposition to their schemes. And they believe rightly: for I have sworn upon the altar of God, eternal hostility against every form of tyranny over the mind of man” - Thomas Jefferson
Anyway, here's how you do it without boost:
If your function is like this:
int myfunction1(int a, char b) { ...}
then you can make an array of pointers to functions that look like myfunction1:
int (*pointers[42])(int,char);

Ok, it's 3:30 AM, so it might be messed up. [wink]
“[The clergy] believe that any portion of power confided to me, will be exerted in opposition to their schemes. And they believe rightly: for I have sworn upon the altar of God, eternal hostility against every form of tyranny over the mind of man” - Thomas Jefferson
Quote:Original post by Tron3k
But functions aren't first-class objects in C++, I think. It must be some kind of specialized syntax. Weird.

No, this requires no special syntax. All you are doing is passing a type as an argument, as I stated.

typedef void poo(int, int);template< typename a >class foo{  typedef a test;};int main(){  foo< void (int, int) > test;}


should work
Since when is void poo(int, int) a proper type, sure that you don't meant void (*poo)(int, int)?
At least GCC won't let me compile that example..
Quote:Original post by doynax
Since when is void poo(int, int) a proper type, sure that you don't meant void (*poo)(int, int)?
At least GCC won't let me compile that example..

void poo(int, int) isn't a proper type, void (int, int) is the proper type. The statement I provided typedefs the void (int, int) type to poo. If GCC doesn't allow it, then it is a fault with the compiler. Try Comeau or VC++ 7.1.

Another fun example that is fully standard that uses function types is:

template< void function() > // Expects a function that returns nothing and takes no parametersclass foo{public:  void do_stuff()  {    /* ... */    function();    /* ... */  }};void bar(){  /* ... */}int main(){  foo< bar > object;  object.do_stuff();}

This topic is closed to new replies.

Advertisement