I've just finished Dietel & Dietel's book on C++, and they couldn't sing the praises of polymorphism enough. But they also said that it comes with an execution speed price tag.
IrrSkript, my scripting engine, should have the ability to process commands. I've thought up two ways of doing this:
(TRADITIONAL) Switch and Callbacks.
Using this, I would check to see if the command from the script matches any commands in the engine. If so, I execute the callback for that command:
SKRIPT: ADDMESH "HelloDonkey"
If (command == "ADDMESH")
(*addmeshcallback)(parameterStack, userData);
(POLYMORPHATIC) VIRTUAL functions
Using this (unless I missed a chapter or made a mistake), I would make a base class, COMMAND, that can be decended (?) from to make command specific classes. COMMAND would have a virtual function do_command(); which is overloaded by all the specific commands. Then all I need is a VECTOR of type COMMAND, which contains all the commands from the script... then I just go down the way, calling do_command(); for every part.
I hope that made sense. I also might wanna do this for the VM part of IrrSkript (the PARSER interprets the commands into IrrSkriptVM, which would allow alternate forms of scripting syntax). All I would have to do, I think, is create class OPCODE, with a virtual function do_operation(), derive opcode specific classes (ADD, SUB, MUL, SHIFT, etc), then push those opcodes into a queue, which I can then process from top down by calling do_operation().
Is this correct?
To do Virtual Polymorphism, or not?
-
- Posts: 602
- Joined: Sat Aug 23, 2003 2:03 am
- Location: Pottstown, PA
- Contact:
The speed difference is negligible, but if you're interested, it's created by the compiler having to store basically an extra field of memory with the program reference in a lookup table, so the code knows to call one function instead of the overriden one.
On all but really old cpu's, the speed is negligible
On all but really old cpu's, the speed is negligible
-
- Posts: 158
- Joined: Wed Apr 28, 2004 11:03 am
- Location: Hungary
- Contact: