-1

I am somewhat new to coding so I don't know very much about compilers and such but I know that the C++ compiler implements a virtual table at runtime so it can dynamically call functions. I am curious as to how this ability was actually implemented. Is it inherent to the compiler or is it a type of program one can tell the compiler to run upon encountering virtual functions? And how might I create such runtime programs for my own use?

Even if it would be impossible to do without creating my own compiler, any explanations to help me understand how the whole system works are greatly appreciated.

  • 2
    Virtual tables are a (nearly universal) implementation detail not specified by the C++ Standard, but it could be as simple as an array of [pointers to functions](https://isocpp.org/wiki/faq/pointers-to-members). – user4581301 May 24 '22 at 19:51
  • You could write your own dispatching functions, but why, when you can use the compiler's version. The compiler has been tested a lot more than your program. Also, only use `virtual` functions as necessary. Not all programs need virtual functions. – Thomas Matthews May 24 '22 at 20:04
  • This question screams for a good book in modern compiler design. These are quite hard to find. But fortunately we already have a good answer to this! https://stackoverflow.com/questions/99297/how-are-virtual-functions-and-vtable-implemented?rq=1 – Captain Giraffe May 24 '22 at 20:20

0 Answers0