Item 19. Commands and Hollywood

When a function object is used as a callback, that’s an instance of the Command pattern.

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 ...

Get C++ Common Knowledge: Essential Intermediate Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.