RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Simplify Callback Dispatching with Enumerated Indexes

Using callbacks instead of direct function calls is a must in event-based applications. This usefulness also extends to other application domains, where they can really make your life easier—that is, until you need to know which pointer is bound to which function and you realize you've forgotten! This month's solution explains how you can use enum types to avoid getting lost in your array of callback pointers.

Callbacks, either in the form of function pointers or pointers to members, offer a significant advantage over direct function calls: the caller doesn't have to know a function's name (nor its object's name, if it's a member function) in order to invoke it. Alas, this anonymity is a fertile source of human errors as programmers might lose track of which pointer is bound to which function. This solution demonstrates a simple and effective technique to overcome this problem.

How to associate meaningful names to indexes of an array of callback pointers?

Use enum types to enumerate array indexes.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date