My main() looks like this:
int main(int argc, char** argv)
{
// Initialize GLUT
glutInit(&argc, argv);
...
glutDisplayFunc(display);
...
// Set robot's parameters
Robot robot; // Initialize global object robot
robot.setSize(50);
robot.setColor('G');
robot.setLocation(50,100);
glutMainLoop();
return EXIT_SUCCESS;
}
Then I have another function, which I would like to have access to the methods of the robot:
// This function is constantly "looped"
void display() {
...
robot.draw();
...
}
What is the legit way to do it in C++?
For anyone interested, the question changed, so my old answer is lost to the edits.
If your
displayfunction is required to have a specific signature (void()), you can usestd::bind, presuming you have access to C++11:If you don’t have C++11,
boost::bindworks just as well:If you have neither, you’ll have to store
robotmore globally.