C++ supports dynamic binding through virtual mechanism. But when i comprehend the virtual mechanism is definitely an implementation detail from the compiler and also the standard just identifies the actions of the items should happen under specific situations. Most compilers implement the virtual mechanism with the virtual table and virtual pointer. Company I know of the salt water evaporates, So my real question is not about implementation detail of virtual pointers and table. My questions are:
- What are the compilers which implement Virtual Mechanism in a other way apart from the virtual pointer and virtual table mechanism? So far as i've come across most(read g++,Microsoft visual studio) implement it through virtual table, pointer mechanism. So practically are there more compiler implementations whatsoever?
sizeofassociated with a class with only an online function is going to be size an pointer (vptr inside
this) on that compiler, So considering the fact that virtual ptr and tbl mechanism is compiler implementation, will this statement I made above be always true?
Edit: Thanks for the solutions men, There's a lot new and interesting information and understanding within the solutions for me personally which i cant help but question which response to accept.
It's not correct that vtable pointers in objects will always be the best. My compiler for an additional language accustomed to use within-object pointers for similar reasons but no more does: rather it utilizes a separate data structure which maps the item address towards the needed meta-data: during my system this is actually shape information to be used through the garbage collector.
This implementation costs a little more storage for any single simple object, is much more efficient for complex objects with lots of bases, which is greatly more effective for arrays, since merely a single entry is needed within the mapping table for those objects within the array. My particular implementation may also discover the meta-data given a pointer to the point interior towards the object.
The particular research is very fast, and also the storage needs very modest, because I'm while using best data structure on earth: Judy arrays.
I additionally are conscious of no C++ compiler using anything apart from vtable pointers, but it's only some of the way. Actually, the initialisation semantics for classes with bases make any implementation untidy. The reason being the entire type needs to see-saw around because the object is built. As a result of these semantics, complex mixin objects result in massive teams of vtables being produced, large objects, and slow object initialisation. This most likely is not due to the vtable technique around requiring to slavishly stick to the requirement the run-time kind of a subobject be correct whatsoever occasions. Really there is no valid reason with this throughout construction, since constructors aren't techniques and should not properly use virtual dispatch: this is not so obvious in my experience for destruction since destructors are really the techniques.
I do not think you will find any modern compilers by having an approach apart from vptr/vtable. Indeed, it might be hard to determine another thing that's not only plain inefficient.
However, there's still quite a large room for design tradeoffs within that approach. Maybe especially regarding how virtual inheritance is handled. So it seems sensible to create this implementation-defined.
If you are looking at this type of stuff, I strongly suggest reading through Within the C++ Object Model.
sizeof classis dependent around the compiler. If you would like portable code, don't make any presumptions.