Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does virtual method invocation work in C++?

How does Virtual Method Invocation work in C++?

like image 429
bunty Avatar asked Sep 27 '10 13:09

bunty


2 Answers

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.

like image 67
Starkey Avatar answered Nov 08 '22 12:11

Starkey


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.

like image 23
sbi Avatar answered Nov 08 '22 12:11

sbi