473,692 Members | 2,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[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::*CACl assFunction)( /*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 RandomMemberFun c( /*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::Random MemberFunc ) );

// Get and call member func
CAFunc *func = (CAFunc*)mTheFu ncs.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 2826

<te*********@BU SThotmailE.Rcom > wrote in message
news:1g0gmt8.y1 2eeuw2ybtkN%te* ********@BUSTho tmailE.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::*CACl assFunction)( /*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 RandomMemberFun c( /*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::Random MemberFunc ) );

// Get and call member func
CAFunc *func = (CAFunc*)mTheFu ncs.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 (*MyFunctionTyp e)(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 FunctionContain er
{
...
addFunc( MyFunctionType fn);
MyFunctionType getFunc( int key);
};

fill those in and ur done.

Yamin
Jul 19 '05 #2
Yamin <ab*****@sdfdas fsd.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
1609
by: christopher diggins | last post by:
What I am trying to answer is, what the most prominent motivation for implenting and using smart pointers in C++ is. Is it primarily to reduce bugs resulting from memory access errors or primarily to permit lazy deallocation designs (i.e. designs that rely on non-determined non-fixed non-explicit deallocation of memory)? Obviously both are advantages, but I want to know if programmers are finding that lazy deallocation is one of the major...
388
21692
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's worth the $25USD. I'm just looking for a book on Pointers, because from what I've read it's one of the toughest topics to understand. thanks in advanced.
102
5979
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV" ( i am talking of unix machine ) while trying to read that location. Then how can i distinguish between a NULL pointer and an invalid location ? Is this essential that NULL pointer should not point to any of the location in the virtual address...
22
4801
by: Christopher Benson-Manica | last post by:
Is adding 0 to a pointer to non-void that is equal to NULL legal? int *p=NULL; p+=0; -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
23
2962
by: junky_fellow | last post by:
I have a question related to pointer/integer conversion. After reading various threads on this newsgroup I found that pointer/integer conversion is not portable and may lead to undefined results even on the same platform. I was just curious to know why is it so ? One obvious reason is that size of pointer variable might not be the same as integer variable. What could be the other reasons ? Consider an implementation where pointers and...
9
5866
by: uotani.arisa | last post by:
Hi, Can someone tell me how to declare a pointer to a vector of pointers? I'm just not sure how to do this... I've tried essentially the following: vector<string *> * v; ....
12
4465
by: bwaichu | last post by:
What is the best way to handle this warning: warning: cast from pointer to integer of different size I am casting in and out of a function that requires a pointer type. I am casting an integer as a pointer, but the pointer is 8 bytes while the integer is only 4 bytes. Here's an example function:
25
13024
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void *p, void *q) that will take any two pointers and decide whether they can be compared or not. I really can't think how to do this - any suggestions?
19
1982
by: MQ | last post by:
Can someone tell me where I should use pointers and where I should use references? In his book, Stroustrup says that you should use pointers for passing arguments that are to be modified, not references. Yet, in an interview, he laments the overuse of pointers and such in code. It seems contradictory to me. Why should we not use references instead of pointers? Can someone give me an idea of best practices in this respect? regards,...
3
1258
by: abhishek | last post by:
Hi group any idea on HOW TO HANDLE POINTERS FROM NON-LOCAL HEAPS?? Thank you
0
8611
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8970
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8812
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8810
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7639
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6462
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5822
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4564
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1962
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.