Passing a member function pointer

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

Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Passing a member function pointer

 
0
  #1
Sep 6th, 2008
Normally I call this function like this

  1. void MouseButton(int button, int state, int x, int y);
  2. ...
  3. glutMouseFunc(MouseButton);

But now I would like to pass a member function instead of a "real" (non-member? whats the word for this?) function.

  1. class Plot
  2. {
  3. void Plot::MouseButton(int button, int state, int x, int y);
  4. ...
  5. Plot() { glutMouseFunc(MouseButton); }
  6. }

But I get this error:
  1. /home/dave/Plot/src/Plot.h:81: error: argument of type 'void (Plot::)(int, int, int, int)' does not match 'void (*)(int, int, int, int)'

What do I have to do differently to get this to work?

Thanks!
Dave
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,624
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 714
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Passing a member function pointer

 
0
  #2
Sep 6th, 2008
Assuming you have control over glutMouseFunc, you need to overload it to accept a pointer to a member function:
  1. class Plot {
  2. public:
  3. void MouseButton();
  4. };
  5.  
  6. void glutMouseFunc ( void (Plot::*pmf)() )
  7. {
  8. //...
  9.  
  10. Plot p;
  11. (p.*pmf)();
  12.  
  13. //...
  14. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Passing a member function pointer

 
0
  #3
Sep 6th, 2008
I actually don't have control over glutMouseFunc (I mean I suppose I could edit the glut source, but that sounds like a bad idea)

Why is it so much different to pass a member function than a normal function?

Is there no way to do this without overloading glutMouseFunc?

Thanks!

Dave
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Passing a member function pointer

 
0
  #4
Sep 6th, 2008
Originally Posted by daviddoria View Post
I actually don't have control over glutMouseFunc (I mean I suppose I could edit the glut source, but that sounds like a bad idea)

Why is it so much different to pass a member function than a normal function?

Is there no way to do this without overloading glutMouseFunc?

Thanks!

Dave
You could just overload the function to accept a pointer-to-member function.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Passing a member function pointer

 
0
  #5
Sep 6th, 2008
As I mentioned, I can't overload the function because it is not mine, it is in the glut library!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Passing a member function pointer

 
0
  #6
Sep 6th, 2008
Non-static member function always called for the object, not in itself. The glutMouseFunc does not know, which object used for member function call. So it's no sense to pass a member function pointer to glutMouseFunc: it can't use such a pointer.

In other words, a pointer to a function is an address but a pointer to a member function is (roughly speaking) an offset in a class member functions table. The gkutMouseFunc wants address only.
Last edited by ArkM; Sep 6th, 2008 at 3:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Passing a member function pointer

 
0
  #7
Sep 6th, 2008
I see, that makes sense.

What I was trying to do was this:

make a Plot class that would have all mouse selection routines (like rotation and zooming) defined and have everything else for opengl setup but then have a virtual Display() function so the user can override what actually gets plotted.

How else would I go about doing this since I have to provide glutMouseFunc and glutDisplayFunc with function pointers to be used when those callbacks are called?

Thanks,
Dave
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,624
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 714
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Passing a member function pointer

 
0
  #8
Sep 6th, 2008
>How else would I go about doing
Use a static member function, or provide a non-member helper that does what you want.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Passing a member function pointer

 
0
  #9
Sep 6th, 2008
ok cool, i'll give it a try

thanks everyone
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 8
Reputation: Athos84 is an unknown quantity at this point 
Solved Threads: 0
Athos84 Athos84 is offline Offline
Newbie Poster

Re: Passing a member function pointer

 
0
  #10
Sep 10th, 2008
Hi all!
I have exactly the same problem! How can I solve it?
I tried to change the member function in a static member function, or to move the member function outside the class, or to use a global wrapper, or to use a delegate function, but I can not reach to a solution :-(

here is my simple code (using the static member function option):
  1. class OGL {
  2. ...
  3. void init(void) {
  4. ...
  5. glutMouseFunc(mouse);
  6. ...
  7. }
  8. static void mouse(int button, int state, int x, int y) {
  9. ...
  10. }
  11. ...
  12. }

what is wrong?
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