24

I read a lot of people writing "a virtual table exists for a class that has a virtual function declared in it".

My question is, does a vtable exists only for a class that has a virtual function or does it also exist for classes derived from that class.

e.g

class Base{
    public:
        virtual void print(){cout<<"Base Print\n";}
};
class Derived:public Base{
    public:
        void print(){cout<<"Derived print\n";}
};

//From main.cpp 
Base* b = new Derived;
b->print();

Question: Had there been no vtable for class derived then the output would not have been "derived print". So IMO there exists a vtable for any class that has virtual function declared and also in classes inheriting from that class. Is this correct ?

2
  • 2
    To complete the experiment, create class derived2 which inherits from derived and also overrides print. Call print on such an instance via a pointer to base... Commented Jan 31, 2010 at 21:52
  • 2
    Learn More About the Vtable @ below Link: learncpp.com/cpp-tutorial/125-the-virtual-table
    – user537597
    Commented Dec 10, 2010 at 8:51

4 Answers 4

20

As far as only virtual-function-specific functionality is considered, in a traditional approach to vtable implementation derived class would need a separate version of vtable if and only if that derived class overrides at least one virtual function. In your example, Derived overrides virtual function print. Since Derived has its own version of print, the corresponding entry in Derived vtable is different from that in Base vtable. This would normally necessitate a separate vtable for Derived.

If Derived didn't override anything at all, formally it still would be a separate polymorphic class, but in order to make its virtual functions work properly we could have simply reused Base vtable for Derived as well. So, technically there wouldn't be any need for a separate vtable for Derived.

However, in practical implementations, the data structure that we usually refer to as "vtable", often holds some additional class-specific information as well. That extra information is so class-specific that most of the time it becomes impossible to share vtables between different classes in hierarchy, even if they use the same set of virtual functions. For example, in some implementations the vtable pointer stored in each polymorphic object points to data structure that also stores so called "RTTI information" about the class. For this reason, in most (if not all) practical implementations each polymorphic class gets its own vtable, even if the virtual function pointers stored in those tables happen to be the same.

2
  • 1
    @AndreyT: Do you know of some webpage that explains the concepts related to vtable for C++ in a good way for a beginner? I am trying to figure why a vtable is needed at all, and how exactly it is implemented.
    – Lazer
    Commented Apr 17, 2010 at 11:09
  • @Lazer implementation details are unspecified. The table is required so at runtime the system knows which version of a polymorphic function to call (this cannot be determined at compile time; consider an operation on a base-class pointer - the operation could be different if the pointer points to an instance of derived class or instance of base class). See en.wikipedia.org/wiki/Virtual_method_table
    – RJFalconer
    Commented Mar 12, 2014 at 10:14
3

Yes, your understanding is correct. Any class that has a base with any virtual functions has a vtable.

3

Yes it's true. Actually, given base's defintion:

class derived:public base{
public:
 void print(){cout<<"derived print\n";}
};

is completely equivalent to:

class derived:public base{
public:
 virtual void print(){cout<<"derived print\n";}
};

... because you already defined print as virtual in base.

I'd wish the compiler would enforce that...

1
  • It is also quite strange that it is possible to compile together a base class with void print() and a derived class with virtual void print()..
    – user6547518
    Commented Jan 22, 2021 at 10:13
2

Yes, that's true. A class inherits all data members from its base class, including the vtable. However, vtable entries are adjusted accordingly (for example if the class overrides a base class virtual method, the corresponding entry in the vtable must point to its own implementation).

But keep in mind that the concept of a 'vtable' is common practice used by vitually every compiler, but it is not compulsory nor standardized.

1
  • The vtable is not a member or anything like a member. Do you mean vptr?
    – curiousguy
    Commented Feb 9, 2017 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.