why runtime polymorphism over static polymorphism?

Jan 21, 2014 at 10:01am
Hi,

I know what are the differences between these two, like in case of runtime plymorphims, the functions will be called based on contents of base class object pointer, here basically compiler doesn't know which member function will be get called, so the compiler arranges for the code to be called at runtime.

But in runtime, c++ uses Vtables, then the runtime env looks for the address of the function to be called and jump to the certain location, basically its slow downs the program(performance hit)..
Other than calling right function at runtime are there any features over compile time polymorphism in runtime polymorphism.

Thanks, in advance.
Last edited on Jan 21, 2014 at 10:02am
Jan 21, 2014 at 10:27am
1. Multiple Inheritance (of interface)
2. Better integration with libraries (all you need to see is the factory)
Jan 21, 2014 at 2:11pm
runtime polymorphism makes sense for the situations where the choice of behavior is, logically, made at run time - based on configs or inputs.

Other than calling right function at runtime are there any features over compile time polymorphism in runtime polymorphism.

Heterogeneous collections are sometimes easier to implement as collections of pointers to polymorphic bases (particularly warranted if those objects are produced by a factory driven by runtime input)
Jan 21, 2014 at 10:13pm
... then the runtime env looks for the address of the function to be called ...


No, the dispatch tables of type-compatible classes are made to have the same layout when the code is compiled, so the program is basically taking the base pointer and adding 'X' to find the function.

... and jump to the certain location, basically its slow downs the program(performance hit) ...

Unless the call is inlined, the jump is going to happen whether you use polymorphism or specialized templates so that part balances out. As far as it's impact on the program, this is an LEA instruction not a "Double Thunk" or anything like that. A solar flare for Sol's closest neighbor would probably have a greater impact on your program's performance </sarcasm?>
Topic archived. No new replies allowed.