What is Callback Function in C++ ?
Posted on February 22nd, 2008
-- Sponsored Links --
“Hollywood Principle” – “Don’t call us; we’ll call you.“
-- Sponsored Links --
What’s a callback? Suppose you’re taking a long trip, and I lend you my car for the purpose. Given the condition of my car, I’ll probably also hand you a sealed envelope with a telephone number in it, along with instructions to call the number in the envelope if you experience any engine problems. That’s a callback.
You do not have to know the number in advance (it may be the number of a good repair shop, a bus line, or the city dump), and in fact you may never have to call the number.
In effect, the task of handling the “engine trouble” event has been partitioned between you (also known as the “framework”) and me (also known as the “client of the framework”).
You know when it’s time to do something but not what to do. I know what to do if a particular event occurs but not when to do it. Together we make a complete application.
Callbacks are a common programming technique and have traditionally been implemented as simple pointers to functions. When a function object is used as a callback, that’s an instance of the Command pattern. A better approach is typically to use a function object rather than a function pointer. Use of a function object—or more typically a function object hierarchy—in conjunction with the Hollywood Principle is an instance of the Command pattern
– from C++ Common Knowledge: Essential Intermediate Programming By Stephen C. Dewhurst
Filed under Programming |
Comments are closed.
