Question

I would like to alter a vector using glut's mouse callback (conditionally adding/erasing to/from it), however I can't pass any extra arguments to the callback function, and glutMainLoop() which handles all the callbacks takes no arguments. The only way I can imagine to circumvent this is to declare the vector globally, but I've heard this is bad practise, so it is my last resort.

Is this a situation when using a global variable? If so, is there a "best way" to do it? If not, how should I approach this problem.

Was it helpful?

Solution

After further research, it seems like I can accomplish this with pointers.

Declare globally a pointer to a vector, and inside the callback call the required methods eg. vector->push_back(x)

In main, create the vector object and assign its address to the global pointer, and attach your callbacks as needed.

OTHER TIPS

You may want to read more about singletons. They are a good way to handle global structures inside a large codebase.

http://en.wikipedia.org/wiki/Singleton_pattern

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top