Callback functions

S

slack_justyb

Well, I'll try to keep this short and sweet.

I'm trying to use glut which is a toolkit for OpenGL. Inside this
toolkit are a couple of functions that are giving me a slight problem
with trying to add to a class.

Here is the header for the class I would like to make (simplified and
error checking removed):

namespace myGlut {

class glutWindow {

public:

glutWindow();
glutWindow(std::string name, int x, int y, int w, int h);
void create();
void redraw(int _w, int _h);
void display();
void keyboard(unsigned char _k, int _x, int _y);
void loop();

private:

std::string title;
int xpos, ypos, width, height;
};
}

Now the details aside the method I'm really looking at here is the
create() method.

Inside this method is a call to a function inside the glut toolkit.
Here is the prototype for that function:

void glutDisplayFunc( void(*func)() );

Which as I see it, takes an address to a function (function pointer and
all that rot.)

However, the problem is that I would like to pass
myGlut::glutWindow::display() as the callback function.

therefore in my code I make a call like so:

void glutWindow::create() {

//Some really boring init stuff.
:
:
//Create a window with data members: title, xpos, ypos, width,
height.
:
:
glutDisplayFunc(myGlut::glutWindow::display);
:
:
//End of method
}

However, a conversion from void(*)() to void(myGlut::glutWindow::*)()
is not possible. Making things static, I believe but correct me if I'm
wrong, would solve the conversion but would really defeat the purpose
of making a class in the first place.

Finally, I just wanted to let everyone know that I am aware of C++
toolkits that wrap OpenGL, but I believe that what I'm asking is more
along the lines of a much broader question of how does one pass a
member function to some of the older C style functions that are looking
for a format of type(*)(...), eh?

Thanks for all the help!

:) Cheers and stuff from justyb's desktop!
 
M

Mike Wahler

However, a conversion from void(*)() to void(myGlut::glutWindow::*)()
is not possible. Making things static, I believe but correct me if I'm
wrong, would solve the conversion but would really defeat the purpose
of making a class in the first place.

Finally, I just wanted to let everyone know that I am aware of C++
toolkits that wrap OpenGL, but I believe that what I'm asking is more
along the lines of a much broader question of how does one pass a
member function to some of the older C style functions that are looking
for a format of type(*)(...), eh?

This is addressed in the FAQ:
http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.2


-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top