Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is the vtable used?

Tags:

c++

Is the vtable only used for virtual function lookups, or does it get used for normal member function lookups as well?

like image 367
John Humphreys Avatar asked Feb 28 '26 04:02

John Humphreys


2 Answers

Technically, this is an implementation detail. The C++ standard says nothing about vtables or vptrs.

But typically, a compiler will only choose to use the vtable/vptr mechanism when a member function is being called in a polymorphic sense (i.e. via a pointer/referene to base class). If it knows at compile-time what to do, then there's no need for the indirection.

like image 186
Oliver Charlesworth Avatar answered Mar 01 '26 17:03

Oliver Charlesworth


Only for virtual function lookups. A non-virtual member function doesn't require much special -- it's just a normal function that receives this as a hidden parameter.

like image 42
Jerry Coffin Avatar answered Mar 01 '26 17:03

Jerry Coffin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!