Search code examples
c++vptr

why do we even need VPTR?


And why don't we use the same method for non virtual functions?

I mean, why do we use virtual functions in that way? Can't we just use them as non-virtaul ones and override them?

And if this method is saving us time/space or what ever, why don't we use the same method for non-virtual functions? I mean it would make sense that there would be one table of functions for a specific class.

Anyway, thanks in advance, I am just a bit confused.


Solution

  • You can't have run-time polymorphism without using a level of indirection. That's what the vptr is for.

    The vptr is not used for non-polymorphic functions because that indirection costs something. The C++ philosophy is that you don't pay for what you don't use.

    EDIT:

    Here's some info on how virtual tables work: http://en.wikipedia.org/wiki/Virtual_table