Passing a member function pointer
Normally I call this function like this
void MouseButton(int button, int state, int x, int y);
...
glutMouseFunc(MouseButton);
But now I would like to pass a member function instead of a "real" (non-member? whats the word for this?) function.
class Plot
{
void Plot::MouseButton(int button, int state, int x, int y);
...
Plot() { glutMouseFunc(MouseButton); }
}
But I get this error:
/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
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
Assuming you have control over glutMouseFunc, you need to overload it to accept a pointer to a member function:
class Plot {
public:
void MouseButton();
};
void glutMouseFunc ( void (Plot::*pmf)() )
{
//...
Plot p;
(p.*pmf)();
//...
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
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
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
As I mentioned, I can't overload the function because it is not mine, it is in the glut library!
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
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.
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
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
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
>How else would I go about doing
Use a static member function, or provide a non-member helper that does what you want.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
ok cool, i'll give it a try
thanks everyone
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
What error are you getting?
The problem with both of those solutions, in my opinion, is that you then cannot access member variables, which was my whole point of doing this in a class! (the display() function can only take certain parameters, so you cannot pass it anything else, you have to make the information global.)
Does this make sense?
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
mouse function is not defined into two files.. what can I do?
That error occurs also if you have implemented the mouse() function in a header file (.h) and include that header file in two or more source files (.cpp). So, you should move theimplementation of the mouse() function to a single source file.
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
Nope, I don't think it can be done the way I would like. I got it to compile with a static member function, but then I can't access non static data members, so that defeats the point!
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204