Hi just a quick question about inlining virtual functions. Say i have a class hierarchy like so:
1 2 3 4 5 6 7 8 9 10 11 12
class AbstractBaseClass
{
virtualvoid myFunc() = 0;
};
class DerivedClass : public AbstractBaseClass
{
void myFunc()
{
cout << "inline or not?";
}
};
Since the base class is abstract, afaik there is no vtable constructed for it, does this mean that if i have a pointer to the base class and i call myFunc() like so:
1 2
AbstractBaseClass * abc_ptr = new DerivedClass();
abc_ptr->myFunc();
If the actual type can be determined by the compiler at compile time, then it might be inlined. if the type cannot be determined, it will not be inlined.
1 2 3
Derived b;
b.myFunc(); // actual type obvious, probably will be inlined
1 2 3
Base* p = new Derived;
p->myFunc(); // actual type can be determined, might be inlined, but isn't as obvious
1 2 3 4 5 6 7 8 9 10 11 12
void f(Base* p)
{
p->myFunc(); // actual type can be determined only if f() is inlined.
// if f() is inlined, then inlining myFunc might be possible
// if f() is not inlined, myFunc() will not be because the type is impossible to
// determine
}
int main()
{
f(new Derived);
}
Thanks for the replies, I guess my question really is if the base class is abstract, does this make it easier for the compiler to determine it's type, since a pointer of type AbstractBaseClass* can never really point to an AbstractBaseClass object? (afaik since you cannot instantiate a AbstractBaseClass object)
No that doesn't help. All that tells the compiler is that the actual type cannot be AbstractBaseClass. It doesn't help it know what the actual type is.
I suppose if you only have one derived class then the compiler might be able to figure it out, but I don't know if compilers go that far.