14

Possible Duplicate:
A question about virtual mechanism in C++

Is using vtable the only way to implement virtual member functions mechanism in C++? What other ways exist?

Community
  • 1
  • 1
sharptooth
  • 159,303
  • 82
  • 478
  • 911
  • Could probably construct some oddball mechanism that would work (global jump table based on embedded type id and function id or something) - but never heard of other actual feasible implementations. – Erik Mar 24 '11 at 10:41
  • 1
    You mean internally? If yes, well, it is the fastest way. You will need an array for fast dispatch and you will be storing pointers. So you will end up with something like vtable always. – Šimon Tóth Mar 24 '11 at 10:42
  • @sharptooth: Duplicate of http://stackoverflow.com/questions/4352032/a-question-about-virtual-mechanism-in-c – Alok Save Mar 24 '11 at 10:59
  • @Als: I searched before asking and didn't find that one. – sharptooth Mar 24 '11 at 11:05
  • @sharptooth: Perhpas the too many questions with similar tags. I could find it just because I was the one who asked that question a while ago. :) – Alok Save Mar 24 '11 at 11:13
  • @Als: I guess the problem is that one is actually two questions (as I see it) and the title is very generic. Just an hour ago I wanted to ask "Do I have to add .dat file from .dbproj Visual Studio project under source control?" and "related questions" showed this one at the top: http://stackoverflow.com/questions/1122151/do-i-need-the-dat-file-when-i-place-a-visual-studio-database-project-under-versi – sharptooth Mar 24 '11 at 11:24

4 Answers4

11

Technically, all that's required for dynamic dispatch is the ability to identify the dynamic type of an object, given a pointer to it. Thus, any sort of hidden (or not-so-hidden) typeid field would work.

Dynamic dispatch would use that typeid to find the associated functions. That association could be a hastable or an array where typeid is the index, or any other suitable relationship. vptr just happens to be the way to achieve this in the least number of steps.

Cubbi
  • 43,318
  • 13
  • 94
  • 159
3

Another possible implementation would be to store the pointers to virtual functions directly into the objects. Of course, this solution is never used in practice (at least in no languages I'm aware of) since it would lead to a dramatic increase of the memory footprint. However, it is interesting to note that a code using this implementation could actually run faster since it removes an indirection layer by suppressing the need for the vptr.

Luc Touraille
  • 72,907
  • 15
  • 82
  • 134
3

Another known mechanism is type dispatch functions. Effectively, you replace the vtable pointer by a typeid (small enum). The (dyanmic) linker collects all overrides of a given virtual function, and wraps them in one big switch statement on the typeid field.

The theoretical justifcation is that this replaces an indirect jump (non-predicatble) by lots of predicatable jumps. With some smarts in choosing enum values, the switch statement can be fairly effective too (i.e. better than lineair)

MSalters
  • 159,923
  • 8
  • 140
  • 320
2

I'm not aware of any compiler which implements virtual functions without using vtable approach.

Theoretically, however, one could create an internal map of object pointers and a table of pointers to virtual functions, something like map<objPtr, functionTable*>, to implement dynamic polymorphism through virtual functions. But then it would make the dynamic dispatching slower than vtable-approach.

It seems vtable-approach is probably the fastest mechanism to implement dynamic polymorphism. Maybe, that is why all compilers employ this approach!

Nawaz
  • 327,095
  • 105
  • 629
  • 812