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?

Chandra Shekhar
  • 598
  • 1
  • 7
  • 25
Blue Diamond
  • 2,979
  • 4
  • 18
  • 21
  • 3
    note that it's `int main`, not `void main`, and class declarations need to end with a `;`. – Sebastian Mach Oct 07 '13 at 12:12
  • 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*](http://www.amazon.com/Inside-Object-Model-Stanley-Lippman/dp/0201834545/) (ISBN: 978-0201834543). – Sean Cline Oct 07 '13 at 12:25

3 Answers3

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.

Santosh Sahu
  • 2,134
  • 6
  • 27
  • 51
  • 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. – Blue Diamond Oct 07 '13 at 12:11
  • 1
    @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. – Santosh Sahu Oct 07 '13 at 12:39
  • 1
    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... – Blue Diamond Oct 07 '13 at 13:56
  • @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. – Santosh Sahu Apr 26 '21 at 05:27
  • @SantoshSahu Does that mean that Each Derived class has its own vptr , or does vptr getting inherited from the Base class to derived class? – Chandra Shekhar May 15 '21 at 16:27
  • @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. – Santosh Sahu May 17 '21 at 20:02
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.

Kos
  • 70,399
  • 25
  • 169
  • 233
  • 1
    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? – Kos Oct 07 '13 at 11:57
  • Maybe the missing link is that Derived's `fun` is also (implicitly) virtual? This is implied because both `fun` type signatures are the same. – Kos Oct 07 '13 at 11:59
  • 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.. – Blue Diamond Oct 07 '13 at 12:15
  • 1
    Non-virtual functions obviously are chosen at compile-time. In the code you've presented, the derived `fun` is also virtual ([implicitly](http://www.drdobbs.com/implicit-virtual/184401933)), because there's a `virtual fun` with same signature in the base class. Change name or add a parameter, see what happens. – Kos Oct 07 '13 at 12:54
  • 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? – Blue Diamond Oct 07 '13 at 14:06
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38727/discussion-between-kos-and-blue-diamond) – Kos Oct 07 '13 at 14:48
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.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • 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... – Blue Diamond Oct 07 '13 at 12:17