0
votes

I want to write a simple class which initializes openGL. I decided to create method run where I initialize opengl, glutDisplayFunc, glutMainLoop. I want to pass draw function to this method and use it as a parameter for glutDisplayFunc

void OpenGL::run(void(*drawFunction())) {
    this->init();
    glutDisplayFunc(drawFunction);
    glutMainLoop();
};

But I've got an error argument of void type *(*)() is incompatible with parameter of type void (*)().

It does not work glutDisplayFunc(&drawFunction); and glutDisplayFunc(*drawFunction); either. What's the problem?

1
typedef void (*callback_function)(void); void OpenGL::run(callback_function param) - LukeCodeBaker
Hust a hint: GLUT doesn't work well with being encapsulated in classes. If you really want to use a class for your main application structure, you should write a number of GLUT wrapper functions that forward the callbacks to the main application object. However I strongly suggest you overthink the whole "app class" approach. By definition there can be only one application instance in a given program. Which means that that application class will always be used as (if it were) a singleton. Singletons however are regarded an anti pattern by many people goo.gl/zU43IF – so why use them? - datenwolf

1 Answers

5
votes

You misplaced a closing parenthesis:

void OpenGL::run(void (*drawFunction)()) {

Function pointers then act like any other pointers, you just pass them by value. You're already doing that correctly: glutDisplayFunc(drawFunction);

The documentation for glutDisplayFunc also show the prototype of the callback: http://www.opengl.org/resources/libraries/glut/spec3/node46.html