# question about passing functions in glut

This topic is 4943 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, I hope I'm not being an asshole by skipping over the FAQ and just posting my question, but I figure its pretty specific and just merits a quick answer. I am also pretty new to graphis programming so bear with me etc. etc. Now then. I am making a terrain map generator/display. In order to avoid having to declare a bunch of global variables and just keep overall neatness at a maximum I tried putting as much as I can in an general GLInterface class (that I wrote). So very basically my program looks like this. class myclass { ///stuff }; class GLInterface { myclass c1; //otherstuff void RenderScene() { //stuff } void setupinterface() { //change window size and other stuff glutDisplayFunc(GLInterface::RenderScene); } startLoop(){ glutMainLoop(); } }; int main() { GLInterface GLI; GLI.setupinterface(); GLI.startLoop(); } However the line glutDisplayFunc(RenderScene); will not complile. It will not compile if I do not scope it with GLInterface:: and it will not compile if I put it in the main glutDispalyFunc(GLI.RenderScene); Please tell me that there is some way short of making RenderScene a global function to get this to work! Thanks a lot. George

##### Share on other sites
The glut callbacks are suppose to be plain non-method, functions. Methods(class-functions) and plain functions do not have the same signature so therefore you can't pass a class method as a function pointer. Try it out without glut. You'll get the same errors.

##### Share on other sites
The only way to bypass your little problem is if the function inside the class were static. Since static methods have the same signature as non-class functions.

##### Share on other sites
Quote:
 Original post by KhaosifixThe glut callbacks are suppose to be plain non-method, functions. Methods(class-functions) and plain functions do not have the same signature so therefore you can't pass a class method as a function pointer. Try it out without glut. You'll get the same errors.

True and not true[smile].
It would work perfectly if the member functions are static. Since they aren't one can use std::mem_fn together with std::bind1st to achieve this (or boost.bind).

Regards,
Pat.

Oh, you already noticed - just ignore the first part [smile]
[/edit]

^ [cool]

##### Share on other sites
Since IMHO the STL is a bit clumsy on the bind1st and mem_fn part, I decided to just post an example:

class GLInterface {     myclass c1;     //otherstuff     void RenderScene() {       //stuff     }     void setupinterface() {      //change window size and other stuff      glutDisplayFunc(std::bind1st(std::mem_fun(&GLInterface::RenderScene), this));     }     void startLoop() {        glutMainLoop();     }};

Hope this helps,
Pat.

##### Share on other sites
thanks a lot, I'll try it right now and see what happens.
I'm afraid I don't know what it would mean to have a static method (I mean I know I could put the word static there but I don't know waht it would do) could someone explain that to me so I could see if that method would be applicable?

##### Share on other sites
darookie, what you recomended std::bind1st(std::mem_fun(&GLInterface::RenderScene), this)
didn't work problem with second argument type as best as I can decipher from the compiler error

##### Share on other sites
Hope this helps :

class CClass{   public :      static GLvoid RenderScene( ) ;    // declare it as static in the class} ;GLvoid CClass::RenderScene( ){ // Important step : define the function outside of the class. Can't be inside.  // Draw stuff.}void main( ){   // Function calls are a little odd. But this is how you call a static method.   CClass::RenderScene( ) ;   }

##### Share on other sites
Quote:
 Original post by Togakangaroothanks a lot, I'll try it right now and see what happens.I'm afraid I don't know what it would mean to have a static method (I mean I know I could put the word static there but I don't know waht it would do) could someone explain that to me so I could see if that method would be applicable?

You could use the PIMPL idiom to achieve that:
class ActualImplementation {private:   // declare datapublic:   // methods   void doSomething();};class PublicInterface {private:    static ActualImplementation *pimpl;public:    PublicInterface() {        if (0 == pimpl) {            pimpl = new ActualImplementation;        }    }    ~PublicInterface() {        delete pimpl;        pimpl = 0;    }    static void doSomething() {        // dispatch to actual implementation        assert (pimpl);        pimpl->doSomething();    }};

You can use PublicInterface::doSomething like a free function now and pass it as a callback to GLUT for example.

Regards,
Pat.

• 37
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631360
• Total Posts
2999556
×