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.
Here’s a simple example: suppose you have parsed a mathematical expression (with the use of the Interpreter pattern, of course!) composed of double values and addition operators:
1 (1.0 + (2.0 + 3.0))
This expression can be presented as a hierarchy similar to the following:
1 struct Expression
3 // nothing here (yet)