January 2019
Intermediate to advanced
512 pages
14h 5m
English
While not very expensive in absolute numbers (a few nanoseconds at most), a virtual function call is several times more expensive than a non-virtual one and could easily be an order of magnitude or more slower than an inline function call. The overhead comes from the indirection: a virtual function is always invoked by a function pointer, and the actual function is unknown at compile time and cannot be inlined.
If the compiler knows the exact function that is going to be called, it can optimize away the indirection and may be able to inline the function.