Xref: utzoo comp.lang.c++:12424 comp.std.c++:760 Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!snorkelwacker.mit.edu!bloom-beacon!eru!hagbard!sunic!mcsun!ukc!icdoc!cc.ic.ac.uk!rb From: rb@cc.ic.ac.uk (Robin Becker) Newsgroups: comp.lang.c++,comp.std.c++ Subject: Simple example needs dynamic types? Message-ID: <1991Mar21.192414.13890@cc.ic.ac.uk> Date: 21 Mar 91 19:24:14 GMT Organization: Imperial College Computer Centre Lines: 27 Originator: rb@cc.ic.ac.uk Nntp-Posting-Host: suns2cc 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