There may be, yes, if you call the member function via a pointer or reference and the compiler can't determine with absolute certainty what type of object that pointer or reference points or refers to. For example, consider:
void f(B* p) { p->foo(); }
void g()
{
D bar;
f(&bar);
}
Assuming the call to f
is not inlined, the compiler needs to generate code to find the location of the A
virtual base class subobject in order to call foo
. Usually this lookup involves checking the vptr/vtable.
If the compiler knows the type of the object on which you are calling the function, though (as is the case in your example), there should be no overhead because the function call can be dispatched statically (at compile time). In your example, the dynamic type of bar
is known to be D
(it can't be anything else), so the offset of the virtual base class subobject A
can be computed at compile time.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…