| | |
Passing a member function pointer
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 628
Reputation:
Solved Threads: 46
Normally I call this function like this
But now I would like to pass a member function instead of a "real" (non-member? whats the word for this?) function.
But I get this error:
What do I have to do differently to get this to work?
Thanks!
Dave
C++ Syntax (Toggle Plain Text)
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.
C++ Syntax (Toggle Plain Text)
class Plot { void Plot::MouseButton(int button, int state, int x, int y); ... Plot() { glutMouseFunc(MouseButton); } }
But I get this error:
C++ Syntax (Toggle Plain Text)
/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
Assuming you have control over glutMouseFunc, you need to overload it to accept a pointer to a member function:
C++ Syntax (Toggle Plain Text)
class Plot { public: void MouseButton(); }; void glutMouseFunc ( void (Plot::*pmf)() ) { //... Plot p; (p.*pmf)(); //... }
I'm here to prove you wrong.
•
•
•
•
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
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.
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.
•
•
Join Date: Feb 2008
Posts: 628
Reputation:
Solved Threads: 46
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
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
•
•
Join Date: Sep 2008
Posts: 8
Reputation:
Solved Threads: 0
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):
what is wrong?
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):
C++ Syntax (Toggle Plain Text)
class OGL { ... void init(void) { ... glutMouseFunc(mouse); ... } static void mouse(int button, int state, int x, int y) { ... } ... }
what is wrong?
![]() |
Similar Threads
- Type comparable (C++)
- passing an array of structs from a member function (C++)
- Help with function (C++)
- Another C++ N00B Begging For Help (C++)
- Stacks using doubly linked lists (C++)
- Passing a Function, function pointer (C++)
- How to be Crash Free (C++)
- Why does my array still empty (C++)
Other Threads in the C++ Forum
- Previous Thread: Static analysis
- Next Thread: pascals triangle
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






