Once you’ve got a hierarchy of types, unless you have access to the source code, it is impossible to add a function to each member of the hierarchy. This is a problem that requires some advance planning and gives birth to the Visitor pattern.
24. Visitor
Get Design Patterns in Modern C++20: Reusable Approaches for Object-Oriented Software Design 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.