View Single Post
Old Feb 9th, 2008, 4:51 PM   #3
Lakrids
Newbie
 
Join Date: Dec 2007
Posts: 28
Rep Power: 0 Lakrids is on a distinguished road
Re: Pointer to member function

Thank you for your reply. However, it's not exactly what I was looking for. I don't think I was clear enough in my question, so here's a simple example :

cplusplus Syntax (Toggle Plain Text)
  1. class Button {
  2. public :
  3. Button(etc, etc, etc, void (*action)());
  4. void onPressed() { *action(); }
  5. };
  6.  
  7. void randomAction() {
  8. // Some code
  9. }
  10.  
  11. int main() {
  12.  
  13. ObjectA A;
  14. ObjectA AA;
  15. ObjectB B;
  16. ObjectC C;
  17.  
  18. Button b(etc, etc, etc, &A.action); // isn't allowed by C++ ISO
  19. Button b(etc, etc, etc, &B.action); // isn't allowed by C++ ISO
  20. Button b(etc, etc, etc, &randomAction); // is allowed by C++ ISO
  21.  
  22. // Other code
  23.  
  24. return 0;
  25. }

So what I want to do, is execute an action ON ANY KIND OF SPECIFIC OBJECT i want, when the button is pressed. Which is not allowed by the C++ ISO, which says :

Quote:
error : ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say ‘&ObjectA::action’
But saying "&ObjectA::action" WILL NOT alter the specific object ("A" in this case), it will only call ObjectA::action(), not A.action().

Hope you guys understand better now

Any suggestions?
Lakrids is offline   Reply With Quote