lapots
lapots

Reputation: 13415

pass function as parameter to a method

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?

Upvotes: 0

Views: 266

Answers (1)

Adam
Adam

Reputation: 17389

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

Upvotes: 5

Related Questions