문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top