Topic : Function Pointers
Author : Jorgen Sigvardsson
Page : << Previous 2  
Go to page :

listMenu->add(new MenuItem('a', "Append data", Handler_app));
This line of code adds one menu item with label "Append data", key 'a' into the menu listMenu. Not only is this item created, but a function address is also associated with it, in this case the address of Handler_app. The menu subsystem registers this address in a function pointer in some internal data structure. Now the menu subsystem may use the function pointer to call Handler_app, when the user chooses the menu item "Append data". The menu subsystem module does not need to know anything about the frontend subsystem, and vice versa. A third module is needed however, one with the knowledge about the two. But this is good since the menu subsystem may very well be replaced by some other system without having to change the frontend, and vice versa. Wouldn't it be nice with a GUI with that list?

Neat, is it not? This method is known as the callback "paradigm". This is one of the most common way to abstract modules and make them less cohesive.

Jorgen Sigvardsson
Last modified: Mon Aug 7 16:04:49 CEST 2000

Page : << Previous 2