--- In [email protected], "Paul Herring" <[EMAIL PROTECTED]> 
wrote:

> Can you not get this method (or create
> another method which calls it) to call
> the equivilant method in the children
> objects in the tree, and pass back
> their results as well?

Yes, I could do that.  That would mean that every state method would 
have to have its own piece of code to iterate over the object's 
children calling the corresponding state method.  For, say, ten 
states, I would have ten iterators with the only difference between 
them being the method called.

I can't help but feel that with all the features of C++, only some of 
which I really understand, there is some simple trick I'm missing to 
express what I want to do quite concisely and simply.

If I were doing this in C, using abstract data types, each "object" 
would have a pointer to the function corresponding to the current 
state and the iterator would call that function indirectly via the 
pointer. The object would manage which function the pointer points to 
when it changes state.  So, there would just be the one iterator used 
in all states.

I could do the same in C++, but it feels kind-of bogus since the 
objects already have pointers to these functions in the vtables and 
all.

What I'm currently thinking is that there should be a dispatcher 
method in the base class and a single iterator that calls that 
instead of trying to call the rigth state method directly.  The 
dispatcher would look at the state and call the right method.  When 
that returns, the dipatcher would iterate over the children calling 
their dispatchers also.  I think this avoids having chunks of code 
that are nearly the same sprinkled about.  It just seems messy having 
to repeatedly exectute the dispatch logic (switch statement) in each 
child as we descend the tree.  We just figured out which method to 
call in the parent, why do we have to start again in the child?

Graham.



Reply via email to