Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Virtual Table C++

Tags:

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 ?

like image 398
Frank Q. Avatar asked Jan 31 '10 21:01

Frank Q.


People also ask

What is virtual table in C?

Virtual Table is a table of function pointers corresponding to the virtual functions introduced by the class. In C, a Virtual Table can be emulated by a structure of pointers-to-functions, as follows: Listing 7 Virtual Table for the Shape Class (see also Figure 3)

What is the use of virtual table?

The virtual table is a lookup table of functions used to resolve function calls in a dynamic/late binding manner. The virtual table sometimes goes by other names, such as “vtable”, “virtual function table”, “virtual method table”, or “dispatch table”.

Which is a virtual table?

A virtual table is an object that presents an SQL table interface but which is not stored in the database file, at least not directly. The virtual table mechanism is a feature of SQLite that allows SQLite to access and manipulate resources other than bits in the database file using the powerful SQL query language.

How does virtual table and VPTR work?

The compiler places the addresses of the virtual functions for that particular class in the VTABLE. In each class with virtual functions, it secretly places a pointer, called the vpointer (abbreviated as VPTR), which points to the VTABLE for that object.


1 Answers

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.

like image 77
AnT Avatar answered Oct 23 '22 06:10

AnT