42

Possible Duplicate:
why do I need virtual table?

What is a vtable in C++?

So far I know that vtable is a virtual table which has an array of pointers to virtual functions. Is there an article I can read with an example of a practical implementation? (Any walk through will be appreciated.)

SuperStormer
  • 4,997
  • 5
  • 25
  • 35
Simsons
  • 12,295
  • 42
  • 153
  • 269

3 Answers3

47

V-tables (or virtual tables) are how most C++ implementations do polymorphism. For each concrete implementation of a class, there is a table of function pointers to all the virtual methods. A pointer to this table (called the virtual table) exists as a data member in all the objects. When one calls a virtual method, we lookup the object's v-table and call the appropriate derived class method.

Community
  • 1
  • 1
doron
  • 27,972
  • 12
  • 65
  • 103
  • 3
    If your base class Foo has a virtual method, then your derived class Bar will have a vtable and the linker will check for that (if missing it says, undefined reference to `vtable for Bar') – Will Jun 28 '17 at 08:35
5

vTable (virtual table) is an implementation detail of dynamic dispatch (virtual methods).

See C++-Lite-Faq for more details.

phadej
  • 11,947
  • 41
  • 78
  • 1
    Broken link, please update or remove. – donturner Dec 04 '17 at 23:58
  • 2
    @donturner IMHO downvoting was rude. Googling C++ Lite Faq gives the page as second result. You could saved me end others trouble by editing the response yourself. After all, the answer is 7 years old. – phadej Dec 07 '17 at 12:51
  • 8
    @phadej link only answers are frown upon for this exact reason: an external resource can move or be delete or changed and an answer here need to be self-sufficient. – bolov Dec 07 '17 at 12:54
  • Link Works! Very Good Article! – Nir Duan Jan 10 '18 at 07:49
  • 2
    This is the very definition of a link-only answer. – AJMansfield Jul 16 '18 at 13:36
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/20313345) – CalvT Jul 16 '18 at 14:03
  • @CalvT I'm very tempted to search the history of guidelines, when the link-only note was introduced. MetaQuestion https://meta.stackexchange.com/questions/92505/should-i-flag-answers-which-contain-only-a-link-as-not-an-answer – phadej Jul 16 '18 at 15:25
  • @phadej I counter your meta post with one of my own - [Should I flag old not-an-answers?](https://meta.stackexchange.com/questions/120658/should-i-flag-old-not-an-answers). – CalvT Jul 16 '18 at 17:08
  • @CalvT cant you just remove whole question as it's marked as a duplicate. I really don't care improving this answer. Added a sentence though. Hopefully makes you happy. – phadej Jul 16 '18 at 18:10
  • @CalvT also the question is "Is there an article with practical implementation?" – phadej Jul 16 '18 at 18:12
  • @phadej then the question is off topic. If you have a problem with me and four others thinking that your answer is not an answer, then by all means write up a meta post and I'll gladly change my views based on the outcome of that. I get this is an old question and answer, but all questions and answers on the site should fit within todays guidelines, not the guidelines of when they where posted. – CalvT Jul 16 '18 at 18:17
-2

For all it's worth, it is not a standard C++ terminology. It is just an implementation detail used by the implementation to implement virtual functions/dynamic binding

Chubsdad
  • 24,777
  • 4
  • 73
  • 129
  • 20
    It's not terminology of the standard, or standardized at all, but it is quite a standard term. Anyway, this would be better as a comment as it doesn't attempt to answer the question. – Potatoswatter Aug 24 '10 at 10:19