To do Virtual Polymorphism, or not?

Discussion about everything. New games, 3d math, development tips...
Post Reply
AutoDMC
Posts: 104
Joined: Sat Sep 18, 2004 3:44 pm

To do Virtual Polymorphism, or not?

Post by AutoDMC »

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?
bal
Posts: 829
Joined: Fri Jun 18, 2004 5:19 pm
Location: Geluwe, Belgium

Post by bal »

Working with callbacks is a more C-ish than using virtual functions. And the speed difference is (imo) negligible.
General Tools List
General FAQ
System: AMD Barton 2600+, 512MB, 9600XT 256MB, WinXP + FC3
Robomaniac
Posts: 602
Joined: Sat Aug 23, 2003 2:03 am
Location: Pottstown, PA
Contact:

Post by Robomaniac »

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
The Robomaniac
Project Head / Lead Programmer
Centaur Force
AssiDragon
Posts: 158
Joined: Wed Apr 28, 2004 11:03 am
Location: Hungary
Contact:

Post by AssiDragon »

I did my own scripting engine a somewhat similiar way - created chains of function pointers...
Staring through eyes of hate we kill
Are we controlled, or is our own will...?
(Edguy)
Post Reply