+ 1

How do v_table and v_ptr work internally?

Are v_tabkes of base class inherited by derived class? Class Base { Public : Virtual void print() { Cout <<"Base print "<<endl; } }; Class Derived : public Base { Public: Void print() { Cout<<"Derived print"<<endl; } }; Int main() { Derived d; Base* b= &d; b->print(); Return 0; } Here is Derived having its own new set of v_table, or is it inheriting Base v_table and appending new virtual funcs in Derived, if any? Will Derived get only the overriden virtual func of the Base or the Base v_table as such?

11th Oct 2021, 12:00 PM
Shilpa
4 Answers
+ 1
/* V-tables (or virtual tables) are how most C++ implementations do polymorphism. For each concrete implementation of a class, there is a table of function pointers to all the virtual methods. A pointer to this table (called the virtual table) exists as a data member in all the objects. When one calls a virtual method, we lookup the object's v-table and call the appropriate derived class method. For all it's worth, it is not a standard C++ terminology. It is just an implementation detail used by the implementation to implement virtual functions/dynamic binding */ Reference from stackoverflow For more info https://practice.geeksforgeeks.org/problems/what-are-vtable-and-vptr
11th Oct 2021, 5:16 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
"Every class that uses virtual functions (or is derived from a class that uses virtual functions) is given its own virtual table." ..thank you sir...
11th Oct 2021, 5:58 PM
Shilpa
0
Refer this post you will get better explanation https://www.programiz.com/cpp-programming/virtual-functions
11th Oct 2021, 3:21 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
0
Cud u pls explain the internal working with v_ptr and v_table?
11th Oct 2021, 4:03 PM
Shilpa