This sample is licensed under the CC0 Public Domain Dedication.
Notify generic observer objects when an event occurs.
The observer pattern allows generic observer objects to be registered with a subject object and receive notifications when certain events occur.
subject class, defined on lines 17–34, contains a
std::vector of references to observers line 33.
Observers (also known as listeners), in this case, are objects that
observer interface (lines 4–8). The
register_observer function (lines 20–23) adds observers
std::vector, which are later to be notified by the
notify_observers function (lines 25–30).