C/C++ casting issue?

Moderator
Posts: 3,572
Joined: 2003.06
Post: #16
Heh, well it won't win any beauty contests, but glad to see you got it working Smile
Quote this message in a reply
Member
Posts: 117
Joined: 2010.09
Post: #17
LOl!
No it won't but it works and is optimal - thats what I wanted :-)))

Your help much appreciated!

Cheers
Quote this message in a reply
Member
Posts: 23
Joined: 2010.08
Post: #18
You don't need to explicitly store the address of a function pointer in your table - it's already a pointer Smile
I believe this might be a cleaner description for your function table:
Code:
static const HIT_FUNC hit_Blast[MAX_NUM_TYPES]=
{
    no_hit,
    wall_hit
};

You don't need to define the table as static and/or const but I like to be clear regarding the intentions of the code. Most likely you are not accessing the table outside the source file (static) or modifying the table during runtime (const).

Here's a test case showing how to call functions from your function table:
Code:
static void test (void)
{
    hit_Blast[0] ("one", "two");
    hit_Blast[1] ("three", "four");
}

... and here's the definitions you'll need if you would like to compile the example yourself:
Code:
typedef const char OBJECT_DEF;
typedef void (*HIT_FUNC) (OBJECT_DEF* ob1, OBJECT_DEF* ob2);

static void no_hit (OBJECT_DEF* ob1, OBJECT_DEF* ob2)
{
    printf ("%s no hit %s\n", ob1, ob2);
}

static void wall_hit (OBJECT_DEF* ob1, OBJECT_DEF* ob2)
{
    printf ("%s wall hit %s\n", ob1, ob2);
}

#define MAX_NUM_TYPES    2

Have fun playing around with function tables - they can be a blast. Just make sure you actually need them first! Just like every solution shouldn't be inheritance in C++, neither should every solution in C be function tables. I quickly skimmed through this article that I found through Google and it looks like a good discussion of function tables.

The Monkey Hustle - Now available on the App Store!
Quote this message in a reply
Member
Posts: 117
Joined: 2010.09
Post: #19
Cheers man, thanks!
Quote this message in a reply
Post Reply