How to call a class member function via function pointer in map [C++]

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2006
Posts: 35
Reputation: Shaitan00 has a little shameless behaviour in the past 
Solved Threads: 0
Shaitan00 Shaitan00 is offline Offline
Light Poster

How to call a class member function via function pointer in map [C++]

 
0
  #1
Jul 6th, 2009
I'm trying to declare a std::map that is keyed by a char and has a function pointer as a second parameter, these functions would be a member function of my class. But for some odd reason I cannot call the functions when extracted...

So, I have a class A as shown below, it contains a map as well as a function

  1. class A
  2. {
  3. private:
  4. map<char, void (A::*)(void)> mapA;
  5. public:
  6. void func();

Next I have main code that looks like this:
  1. A::A() // constructor
  2. {
  3. // Generate User Options
  4. mapA['a'] = &A::func;
  5. }

Now, I try to execute func by extracting it from the map and running the function:
  1. map<char, void (A::*)(void)>::iterator it;
  2. for ( it=mapA.begin() ; it != mapA.end(); it++ )
  3. {
  4. (*it).second();
  5. }

I would assume this would launch function "func" as it is stored in the map, but instead it generates the following error message:
error C2064: term does not evaluate to a function taking 0 arguments

Any help would be greatly appreciated...
Thanks,
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 461
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: How to call a class member function via function pointer in map [C++]

 
0
  #2
Jul 6th, 2009
It works with static method only.
Last edited by adatapost; Jul 6th, 2009 at 4:06 am.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: How to call a class member function via function pointer in map [C++]

 
0
  #3
Jul 6th, 2009
Last edited by jencas; Jul 6th, 2009 at 5:13 am. Reason: link added
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: How to call a class member function via function pointer in map [C++]

 
0
  #4
Jul 6th, 2009
try the following.

the syntax seems difficult but look it carefully.
  1. (f.*((*it).second))();
  2. (f.*(it->second))();

the easier way is to create the typedef and assign the return value of m['a'] to that, and then call that pointer to member function.

like below.
  1. typedef void (A::*FMemPtr)(void);
  2.  
  3. map<char, FMemPtr> m;
  4. m['a'] = &A::func;
  5.  
  6. FMemPtr funcPtr = m['a'];
  7. A obj;
  8. (obj.*funcPtr)();
same goes for iterator.

hope this helps
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: How to call a class member function via function pointer in map [C++]

 
0
  #5
Jul 6th, 2009
by the way following is also a legal syntax.
  1. (obj.*m['a'])();

.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC