I have some C++ code (written by someone else) which appears to be calling the wrong function. Here\'s the situation:
UTF8InputStreamFromBuffer* cstream = foo();
Based on the assembly, it seems pretty clear that the binding is dynamic and from the first entry of the vtable. The question is which virtual table!?! I would suggest you use a static_cast
instead of a C-style cast (of course, @VJo: dynamic_cast
is not needed in this case!). There is nothing in the standard that requires a pointer BadDocumentReader* ptr
to have the same actual value (address) as its cast static_cast
. This would explain why it binds the call to the first entry of the vtable of BadDocumentReader
and not to the vtable of its base class. And, btw, you shouldn't need a cast at all in this case.
One possibility that doesn't really agree with the asm, but still good to know.
Because you create the BadDocumentReader
in the same scope as you are calling the reader->readDocument
, the compiler gets a little too clever and decides that it can resolve the call without having to look it up in the vtable dynamically. This is because it knows that the "real" type of the reader pointer is actually BadDocumentReader
. So it bipasses the vtable and binds the call statically. At least, that is one possibility which I had happen to me in an almost identical situation. Based on the asm, however, I'm pretty sure the first possibility is the one occurring in your case.