Mechanism of Vptr and Vtable in C++
Asked Answered
C

3

15

In C++, during dynamic binding, consider the following example...

class Base
{
  virtual void fun()
  {
     cout<<"Base";
  }      
};

class Derived : public Base
{
   void fun()
   {
     cout<<"Derived";
   }
};

int main()
{
  Base *bptr;
  Derived d;
  bptr=&d;
  bptr->fun();
  return 0;
}

The output of the above C++ program is "Derived" due to the declaration of virtual keyword/dynamic binding.

As per my understanding, a virtual table (Vtable) would be created which contains the address of the virtual functions. In this case the virtual table created for the derived class points to the inherited virtual fun(). And bptr->fun() will be getting resolved to bptr->vptr->fun();. This points to the inherited base class function itself. I am not completely clear on how the derived class function is called?

Cutwork answered 7/10, 2013 at 11:49 Comment(2)
note that it's int main, not void main, and class declarations need to end with a ;.Trixie
The answers below look good but if you feel the need to read more on the subject, I'd recommend Inside the C++ Object Model (ISBN: 978-0201834543).Silverware
P
15

Just went through this link virtual table and _vptr

It says that the workflow will be like ..

  1. base_ptr->base_vptr----> to check the access of virtual function in base class.

  2. base_ptr->derived_vptr->virtual_function()---> to call/invoke the virtual function.

Hence the derived class virtual function is called.. Hope you find it helpful.

Pigfish answered 7/10, 2013 at 12:5 Comment(6)
Referred this link already... Doesnot provide the complete soln. Your point 2 says that virtual function will be called, here is the problem. But in this case derived has to be called.Cutwork
@BlueDiamond: base_ptr->derived_vptr->virtual_function()---> to call/invoke the virtual function in derived class.. Base class pointer would contain the derived virtual pointer(derived class virtual pointer points to derived class virtual table). Then derived virtual pointer when invokes the virtual function, the derived class virtual function is invoked.Pigfish
The derived virtual function shouldn't be invoked.. The derived class virtual function is inherited from base class.. The overridden function is supposed to be called...Cutwork
@BlueDiamond, if you don't define the virtual function in derived class, then the base class virtual function will be invoked, but since you are overriding it, here the derived class virtual function is invoked.Pigfish
@SantoshSahu Does that mean that Each Derived class has its own vptr , or does vptr getting inherited from the Base class to derived class?Colston
@ChandraShekhar, yes each derived class has its own vptr and points to vtable. It can't be inherited because during intitialization of object, the vptr points to the 1st entry of class vtable. The vtable entries contains function pointer and vtable can have an entry of base class function pointer, if the same class is not overridden in the derived class. Hope it clarifies you.Pigfish
A
3

And bptr->fun() will be getting resolved to bptr->vptr->fun();. This points to the base class function itself.

Wrong. The Derived instance's vptr (a hidden field in each instance) points to the Derived vtable.

A answered 7/10, 2013 at 11:51 Comment(6)
Ok, so you understand that each class has its own vtable (that says which implementation should be used for each virtual method) and each instance's vptr points to the correct vtable, matching the object's actual (dynamic) type. What is missing?A
Maybe the missing link is that Derived's fun is also (implicitly) virtual? This is implied because both fun type signatures are the same.A
Vtable contains the address of virtual functions only. But how the derived function will be called? The vtable doesn't contain the address of non-virtual function..Cutwork
Non-virtual functions obviously are chosen at compile-time. In the code you've presented, the derived fun is also virtual (implicitly), because there's a virtual fun with same signature in the base class. Change name or add a parameter, see what happens.A
The above code is giving the expected o/p. But I am not clear about the indepth mechanism. There are two functions with same names/signatures in derived class. Say, the one is inherited virtual fun() and the other is over-ridden non virtual fun(). The derived class V-table contains the address of inherited virtual fun() only rit? Then how the other function is being called, ie non virtual?Cutwork
let us continue this discussion in chatA
K
0

The Standard doesn't specify the mechanism by which polymorphism is implemented. All the Standard says is how it should work -- not how compiler vendors should implement it.

That being said, you have it pretty much right as far as GCC under Linux and MSVC under Windows is concerned, and I would expect most other compilers to be similar.

Keep answered 7/10, 2013 at 11:53 Comment(1)
Makes sense..Vtable contains the address of virtual functions only. But how the derived function will be called? The vtable doesn't contain the address of non-virtual functions rit.. I would like to get this clarified...Cutwork

© 2022 - 2024 — McMap. All rights reserved.