Search code examples
c++vtable

pure virtual class, only 1 derived class, still vtable?


my question is rather simple, if you have an pure virtual class (interface) but due the current active build, only 1 derived class is compiled, will there still be a vtable created ?

class Foo
{
    virtual void bar() = 0;
}
#if 1
class Foo_1 : public Foo
{
void bar() {cout<<"foo_1";}
}
#else
class Foo_2 : public Foo
{
void bar() {cout<<"foo_2";}
}
#endif

their is (as far as I can tell) no need for a vtable in this case.

So will this get optimised or do you still get the overhead of the vtable ?

thx


Solution

  • You'll still get the vtable. After all, the compiler doesn't know if class Foo will be used in some other translation unit. Or maybe you'll package it up as a library and some other user will derive from Foo; again, the compiler doesn't know a priori.