473,666 Members | 2,044 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 2824

<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
1608
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
21665
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
5972
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
4799
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
2961
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
5865
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
4462
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
13020
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
1979
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
1257
by: abhishek | last post by:
Hi group any idea on HOW TO HANDLE POINTERS FROM NON-LOCAL HEAPS?? Thank you
0
8444
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
8869
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
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...
0
7386
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...
0
5664
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
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1775
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.