views:

168

answers:

3

How does Virtual Method Invocation work in C++?

+5  A: 

Through virtual tables.

Read this article, http://en.wikipedia.org/wiki/Virtual_table.

I could explain it here, but the wikipedia does a better job than I could.

Starkey
After reading that page I think it is horrible for beginners. It does not contain enough information to be useful yet to much that it is dangerous. The section on efficiency is downright misleading and the paper it links to is from 1996 (that's over a decade old and from the time of gcc 2.96 (that's not even considered a working C++ compiler let alone a modern C++ compiler)). The section on alternatives is good for a relative discussion of virtual dispatch, **BUT** absolutely non of it applies to C++ (which is not obvious to a beginner).
Martin York
A: 

Every class with at least one virtual method has it's virtual table - table of pointers to functions that are that class's methods.

It's extensively used in COM.

Daniel Mošmondor
Every class does NOT have a virtual table. First of all, only polymorphic class would have a vtable, and second of all vtables are implementation-specific. The Standard just says "thou shalt work," it doesn't say how, and vtables are how.
John Dibling
+3  A: 

The C++ standard doesn't specify how the virtual function mechanism should be implemented.

That said, I think all current C++ compilers use virtual tables.
The common way to do this for classes which contain at least one virtual function to have a hidden pointer to a so-called virtual table, where the addresses of the virtual functions for a specific class are entered in compiler-specific order.
Each constructor will then set this hidden pointer to the virtual table of the class it belongs to.

sbi
Actually, virtual table is the simplest method, but others have been tried and reported as being more efficient (both in execution time and binary size), for example see: http://groups.csail.mit.edu/pag/paste2005/slides/privat-paste2005-slides.pdf it's not very detailed but there are cool graphs at the end which shows that virtual table is the least efficient method of those compared.
Matthieu M.
Agree that VT is the simplist (one of the reasons that it is the most widely used). But those graphs (wow), the lines are way to flat there is something funky going on there. Also the line "Small programs are generated by a script" sticks out a bit. I would have liked to see real world programs implemented in all three languages. Also different types of program are going to have different characteristics.
Martin York
@Matthieu: Wow, I learned something new again! Thanks, i adapted my answer accordingly. I hope it's alright now?
sbi
@Martin: I'd definitely like to see that at work too. However it's nice to that alternatives are considered. I think the main issue encountered by the `vtable` approach is with multi-inheritance, I'd like to see such graphs in the much more common case of simple-inheritance, where the `vtable` approach looks quite optimal.
Matthieu M.
@Matthieu: Yes I think we can agree that it is all good work. We just need more information.
Martin York
Matthieu M.
@Martin: I found another document (finally) which describes several ways of implementing dispatching (multiple) --> http://www.lirmm.fr/~ducour/DEA/Papiers02/OOPSLA%2702-dispatching-TS.pdf (Chapter 2).
Matthieu M.