Jump to content
  • Advertisement


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


Table of pointers to member functions

This topic is 5653 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.

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

Share this post

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


// * OR *

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


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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!