Jump to content
  • Advertisement
Sign in to follow this  
Dospro

array of routine pointers

This topic is 5047 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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]

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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]

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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 parameters
class foo
{
public:
void do_stuff()
{
/* ... */
function();
/* ... */
}
};

void bar()
{
/* ... */
}

int main()
{
foo< bar > object;
object.do_stuff();
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!