I have a functioning C++ callback function, triggered by a user ‘mouse down’ event. (The IDE is VS2010.)
With each call, I’d like to increment a simple count variable that is local to the callback’s scope. Simply put, what is the ‘best practices’ way to do this?
Thanks in advance for any opinions or directives.
Replace your callback function with a functor – they can store state. An example functor:
Note the use of a
shared_ptrinside the functor – this is becausefhas a local copy of the functor (note the pass-by-value) and you want that copy to share itsintwith the functor to which you have access. Note also thatfhas to take its argument by value, since you want to support all callables, and not just functors.