Skip to main content
gamedev.net
🔒 Locked

Downcasting

Started by King of Men 0 replies 800+ views
Original Post
King of Men
King of Men
Why does downcasting have a runtime performance cost? It seems to me that this should be taken care of by the compiler, for the following reason : When the compiler sees the downcast, it knows that from now on it needs to use the virtual table belonging to the subclass. But it knows perfectly well where it put that table, so all it needs to do is add an offset to its method calls. Now, I understand that this theory is not correct, but I don't understand why. Could someone explain it to me?
To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
Palidine
Palidine
If you call virtual functions downstream of the downcast you still have to call the derived class's version. If you discard the derived class's virtual table you will be executing base class implementations of the virtual functions (which could be pure virtual) rather than the _correct_ derived class versions.

-me

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.