I have a base class ‘Node’ which contains a list of child nodes. Node defines a ‘forEachNode’ function which takes a callback as a parameter and calls it on each node in the hierarchy.
I have a class derived from Node – ‘SpecialNode’ (not really a name I’d choose – just an example!). Node knows nothing about SpecialNode.
I want to iterate over just the SpecialNodes. I’ve got to the point where SpecialNode defines a callback to pass to ‘forEachNode’, but I’m not sure how to call the callback for SpecailNodes only. Any advice?
You can try something like:
Or you put a virtual function in Node in order to be called from the callback which you can implement differently in the subclasses.