Possible Duplicate:
An issue about virtual mechanism in C++

Is applying vtable the only method to implement virtual member functions mechanism in C++? The other ways exist?

Technically, all that's needed for dynamic dispatch is a chance to identify the dynamic type of the object, given a pointer into it. Thus, any kind of hidden (or otherwise-so-hidden) typeid area works.

Dynamic dispatch would use that typeid to obtain the connected functions. That association might be a hastable or perhaps an array where typeid may be the index, or other appropriate relationship. vptr just is actually the best way to accomplish this whatsoever quantity of steps.

I am unaware of any compiler which implements virtual functions without needing vtable approach.

Theoretically, however, you could create an interior map of object pointers and a table of pointers to virtual functions, something similar to map<objPtr, functionTable*>, to implement dynamic polymorphism through virtual functions. However it might result in the dynamic dispatching reduced than vtable-approach.

It appears vtable-approach is most likely the quickest mechanism to implement dynamic polymorphism. Maybe, that's why all compilers use this approach!

Another possible implementation is always to keep pointers to virtual functions into the objects. Obviously, this option would be not used at all used (a minimum of in no languages I am conscious of) because it would result in a dramatic increase from the memory footprint. However, it's interesting to notice that the code by using this implementation could really improve your speed because it removes an indirection layer by controlling the requirement for the vptr.

Another known mechanism is type dispatch functions. Effectively, you replace the vtable pointer with a typeid (small enum). The (dyanmic) linker collects all overrides of the given virtual function, and systems them in a single large switch statement around the typeid area.

The theoretical justifcation is this fact replaces an indirect jump (non-predicatble) by plenty of predicatable jumps. With a few inteligence in selecting enum values, the switch statement could be fairly effective too (i.e. much better than lineair)