Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Horn

Table of pointers to member functions

This topic is 5484 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

I''m writing my own virtual machine (who isn''t these days?) and I''m trying to figure out an elegent way of handling all the opcodes. I''d figure I''d use a table of functions so I would be able to simply do HandleOp[OP_CODE](param). I want the functions to be able to access member variables within the class therefore they need to be member functions. What I can''t figure out is how to create a table. I''ve been able to do it using global functions using the following method but I can''t get the syntax right for member variables.
void(*funcTable[10])(int);

funcTable[0] = Do_This;
funcTable[0](5);
I''m using MSVC7 if it makes a difference.

Share this post


Link to post
Share on other sites
Advertisement
This works for me:


#include <stdio.h>
typedef void(*p)(int);void t(int i){printf("%d",i);}main(){p f[5];f[0]=t;f[0](3);return 0;}

Share this post


Link to post
Share on other sites

typedef RETURN_TYPE(Class::*MbrFuncPtr)(ARGUMENTS);

MbrFuncPtr apFuncs[10] = { &Class::FuncOne, &Class::FuncTwo, ... };

void TestFunction()
{
Class oInstanceToUse;
// Make a call

(oInstanceToUse.*(apFuncs[1]))(ARGUMENTS);

// * OR *


Class *pInstanceToUse; // Or inside a class, use this ptr

(pInstanceToUse->*(apFuncs[1]))(ARGUMENTS);
}


Parentheses are important when making the call, otherwise you'll get weird compiler errors. The above should work though.

EDIT: Added pointer example also

[edited by - cgoat on June 19, 2003 1:50:31 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!