Topic: Simple example needs dynamic types?
Author: rb@cc.ic.ac.uk (Robin Becker)
Date: 21 Mar 91 19:24:14 GMT Raw View
Hi, I'm just learning C++ and am working my way through various examples.
I have been following the various discussions on whether run time type tags
should be available or not. I have a simple example where it seems that the
C++ model would seem to be inadequate and would like to know if this is true
or not.
I am considering the construction of a stack based interpreter with
heterogeneous stack values. Now it's easy to use this stack to do simple
things like printing a reverse list using an overloaded member function, but
it seems to be impossible for me to use OOP techniques to do arithmetic when
the stack value types are different. I would like to be able to code like
switch(operator){
.....
case ADD: st.push(st.pop()+st.pop()); break;
case MUL: st.push(st.pop()*st.pop()); break;
.....
}
but since the pop() function returns only the hetero stack type it seems that
the various overloaded operators (+ - * etc.) must determine internally what
kind of values they're operating on. This leads to some kind of type information
being required in the stack element type and each operator will then contain
switch statements to handle the various value type combinations. All my
texts say it's easy to provide overloaded operators for say rationals and
integers, but don't say how to handle this type of situation where the
static type information has been `lost'. Am I missing something or is this
really as difficult as I make out. Robin Becker