469,645 Members | 1,971 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

[Q] Pointers to Non-Static class member functions

I know that one cannot cast a non-static member function to a void *.
(http://users.utu.fi/sisasa/oasis/cpp...o-members.html)

However, I have a case where I need to call a certain member function
based on a value passed into another function.

Since there are a large number of these values and therefore a large
number of potential functions I need to call, I wanted to avoid using a
massive switch statement (or equivalently, if/elseif) and to store these
functions in an efficient data structure so I can extract the member
function to call based on the value and then call it.

Now, I come up with a design that I believe will work and would be
perfectly legal, but I wanted to check with some of the fine folks here
just to make sure.
class CAClass;

typedef long (CAClass::*CAClassFunction)( /*params*/ );

class CAFunc
{
CAFunc( CAClass *obj, CAClassFunction func );
~ CAFunc( void ) {}

long Call( /*params*/ ) { return (mObj->*mFunc)( /*params*/ ); }

protected:

CAClass *mObj;
CAClassFunction mFunc;

private:
};
class CFuncs
{
public:

CFuncs( void );
virtual ~ CFuncs( void );

virtual void AddFunc( const long key, void *func );
virtual void *GetFunc( const long key );

protected:

/* storage var here */

private:
};
class CAClass
{
public:

virtual long RandomMemberFunc( /*params*/ );

protected:

CFuncs mTheFuncs;
};
So, inside of an instance of CAClass, I should be able to do:

// Add a member func
mThefuncs.
AddFunc( uniqueKey,
new CAFunc( this, &CAFunc::RandomMemberFunc ) );

// Get and call member func
CAFunc *func = (CAFunc*)mTheFuncs.GetFunc( uniqueKey );
if ( func )
returnValue = func->Call( /*params*/ );
I've left out various details, but nothing that should be vital.

So, everything look legal?

Assuming it is legal, out of curiosity, Would you consider such a thing
just a "bad idea" and use the massive switch statement anyway?

--
Jul 19 '05 #1
2 2675

<te*********@BUSThotmailE.Rcom> wrote in message
news:1g0gmt8.y12eeuw2ybtkN%te*********@BUSThotmail E.Rcom...
I know that one cannot cast a non-static member function to a void *.
(http://users.utu.fi/sisasa/oasis/cpp...o-members.html)

However, I have a case where I need to call a certain member function
based on a value passed into another function.

Since there are a large number of these values and therefore a large
number of potential functions I need to call, I wanted to avoid using a
massive switch statement (or equivalently, if/elseif) and to store these
functions in an efficient data structure so I can extract the member
function to call based on the value and then call it.

Now, I come up with a design that I believe will work and would be
perfectly legal, but I wanted to check with some of the fine folks here
just to make sure.
class CAClass;

typedef long (CAClass::*CAClassFunction)( /*params*/ );

class CAFunc
{
CAFunc( CAClass *obj, CAClassFunction func );
~ CAFunc( void ) {}

long Call( /*params*/ ) { return (mObj->*mFunc)( /*params*/ ); }

protected:

CAClass *mObj;
CAClassFunction mFunc;

private:
};
class CFuncs
{
public:

CFuncs( void );
virtual ~ CFuncs( void );

virtual void AddFunc( const long key, void *func );
virtual void *GetFunc( const long key );

protected:

/* storage var here */

private:
};
class CAClass
{
public:

virtual long RandomMemberFunc( /*params*/ );

protected:

CFuncs mTheFuncs;
};
So, inside of an instance of CAClass, I should be able to do:

// Add a member func
mThefuncs.
AddFunc( uniqueKey,
new CAFunc( this, &CAFunc::RandomMemberFunc ) );

// Get and call member func
CAFunc *func = (CAFunc*)mTheFuncs.GetFunc( uniqueKey );
if ( func )
returnValue = func->Call( /*params*/ );
I've left out various details, but nothing that should be vital.

So, everything look legal?

Assuming it is legal, out of curiosity, Would you consider such a thing
just a "bad idea" and use the massive switch statement anyway?

--


Hey there,

I have a number of concerns with this. First of all, are the return values
the same for all the functions? Are the parameter types the same for all
functions.

I have no idea how u'd pass different param types to different functions
without switching/ifing. I assume they are, or ur description above would
not work. The normal thing then to do is create a function pointer.

typedef int (*MyFunctionType)(int param1, bool param2);

Then just add some kind of container class which holds a bunch of these and
is indexable by a key...maybe a map or a hashtable...or even a vector if the
keys are 'nice'.

class FunctionContainer
{
...
addFunc( MyFunctionType fn);
MyFunctionType getFunc( int key);
};

fill those in and ur done.

Yamin
Jul 19 '05 #2
Yamin <ab*****@sdfdasfsd.com> wrote:
I have a number of concerns with this. First of all, are the return values
the same for all the functions? Are the parameter types the same for all
functions.


Yes, which is why I even considered doing something like this to begin
with.

Thanks for the confirmation.

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by christopher diggins | last post: by
388 posts views Thread by maniac | last post: by
102 posts views Thread by junky_fellow | last post: by
22 posts views Thread by Christopher Benson-Manica | last post: by
23 posts views Thread by junky_fellow | last post: by
9 posts views Thread by uotani.arisa | last post: by
12 posts views Thread by bwaichu | last post: by
25 posts views Thread by J Caesar | last post: by
19 posts views Thread by MQ | last post: by
3 posts views Thread by abhishek | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.