Are there any compilers that implement Virtual Mechanism in any other way than the virtual pointer and the virtual table engine? As far as I saw most (read g ++, Microsoft visual studio) implement it through a virtual table, a pointer mechanism. Are there practically any other compiler implementations?
All current compilers that I know of use the vtable mechanism.
This is an optimization that is possible because C ++ is checked statically.
In some more dynamic languages, instead, there is a dynamic search through a chain of base classes, a search for the implementation of a member function, which is called virtually, starting with the most derived object class. For example, how it works in the original Smalltalk. And the C ++ standard describes the effect of a virtual call, as if such a search were used.
At Borland / Turbo Pascal in 1990, this dynamic search was used to search for Windows API window message handlers. And I think maybe the same thing in Borland C ++. This was in addition to the regular vtable mechanism used exclusively for message handlers.
If it was used in Borland / Turbo C ++ - I can’t remember - then it supported language extensions that allowed you to associate a message identifier with message handler functions.
The size of any class with a virtual function will be the size of the pointer (vptr inside this) in this compiler. Therefore, given that the virtual mechanism of ptr and tbl itself is an implementation of the compiler, will this statement I made above is always true?
Formally, no (even with the assumption of the vtable mechanism), it depends on the compiler. Since the standard does not require a vtable mechanism, it does not say anything about placing a vtable pointer in every object. And other rules allow the compiler to freely add padding, unused bytes at the end.
But in practice it is possible .; -)
However, this is not something you should rely on, or what you need to rely on. But in a different direction, this may be required, for example, if you define an ABI. Then any compiler that does not do this simply does not meet your requirements.
Cheers and hth.,
Cheers and hth. - Alf Dec 07 '10 at 11:31
source share