tags:

views:

47

answers:

1

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?

+1  A: 

You can try something like:

if (dynamic_cast<SpecialNode*>(n) != NULL) {
    do_something();
}

Or you put a virtual function in Node in order to be called from the callback which you can implement differently in the subclasses.

boutta
Both sound good to me - thanks!
Tim Gradwell