6

How does Virtual Method Invocation work in C++?

bunty
  • 2,665
  • 8
  • 30
  • 27
  • Eh? You mean calling a virtual method? It's the same as calling a regular one. Make your question more specific. – Alexander Rafferty Sep 27 '10 at 13:11
  • 1
    **In C++** invocation is done just like invocation of a normal method `object.foo();` Let me guess. You ask about implementation? – Maciej Hehl Sep 27 '10 at 13:12
  • 1
    Implementation of Virtual Methods is undefined so the question is meaningless. As a side note knowing the answer out of context without experience will not help you in any way (unless you want to build a compiler and then you would not be asking the question anyway). – Martin York Sep 27 '10 at 14:07

4 Answers4

8

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
  • 9,673
  • 6
  • 31
  • 51
  • 2
    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 Sep 27 '10 at 15:12
  • over 2 decades now – talekeDskobeDa Nov 26 '19 at 08:21
5

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
  • 219,715
  • 46
  • 258
  • 445
  • 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. Sep 27 '10 at 15:36
  • 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 Sep 27 '10 at 16:47
  • @Matthieu: Wow, I learned something new again! Thanks, i adapted my answer accordingly. I hope it's alright now? – sbi Sep 27 '10 at 17:42
  • @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. Sep 27 '10 at 19:02
  • @Matthieu: Yes I think we can agree that it is all good work. We just need more information. – Martin York Sep 27 '10 at 19:08
  • @Martin: apparently it seems the prominent feature is the colored table, I found a paper on the ACM related to this: http://portal.acm.org/citation.cfm?id=74900&dl=GUIDE&coll=GUIDE&CFID=106313326&CFTOKEN=45282576 however I don't have an ACM account. The idea is to get only one virtual table even when MI is involved. I still don't understand how it's possible without a whole program analysis though. – Matthieu M. Sep 27 '10 at 19:44
  • @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. Sep 27 '10 at 20:02
0

With VTables and function pointers. Virtual functions' function pointer will be listed in VTable
MFC is using Message Map instead of Virtual function, which reduces the size limitation. If we use several virtual function VTable will end up with big size.

Carthi
  • 265
  • 4
  • 7
0

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
  • 19,718
  • 12
  • 58
  • 99
  • 3
    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 Sep 27 '10 at 13:24