Reputation: 39294
Is the vtable only used for virtual function lookups, or does it get used for normal member function lookups as well?
Upvotes: 2
Views: 863
Reputation: 6181
This is always implementation dependent, but most of the compilers will use vtable when:
virtual
function is being called;virtual
-y inherited class is being accesed.virtual
-y inherited class access includes both virtual and non-virtual method calls, but also field access, and even pointer casting
class foo{
public:
virtual void bar(){}
};
class foo2: public virtual foo{
public:
virtual void bar2(){}
};
int main(int argc, char *argv[])
{
foo2* f2= new foo2();
f2->bar();
foo* f1 = f2;
}
Line foo* f1 = f2;
will also read vtable (although firstly check will be made for f2==NULL, in such case f1 will be also NULL).
Upvotes: 0
Reputation: 19
And method pointer lookup. There is at least a check to see if the pointer points on virtual member function.
Upvotes: -1
Reputation: 25763
As far as I know, the vtable is only created (and thus used) when that method being called is specified as Virtual. If it's virtual, then it will use the vtable, if it is not virtual, then it will be statically binded.
This determination is done at compile time.
Upvotes: 0
Reputation: 272517
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.
Upvotes: 9
Reputation: 2359
The standard doesn't dictate how inheritance is implemented, so a vtable doesn't necessarily exist. But as far as I know all the current major compilers only use vtables to call virtual functions.
Upvotes: 1
Reputation: 490158
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.
Upvotes: 3