Run time Polymorphism

In below code,Because of virtual function in Base class Virtual Table will created by compiler and also Virtual pointer created in constructor while object creation.

But In Derived class there is no virtual function, but its Base class has virtual function. So my question is there will be Virtual table crated for Derived class, If /yes the what will it contains> i.e. address of Base class virtual function or Derived class non-virtual function?

class Base
{
int a;
public:
virtual void Fun(){}
};

class Derived : public Base
{
int b;
public:
void Fun(){}
};

int main()
{
//
Base* ptr = new Derived();
ptr->Fun(); // Derived Fun() will get call
}
Derived::Fun() is a virtual function. It is automatically made virtual because Base::Fun() is a virtual function.
Last edited on
Building on Peter87's correct statements, Derived will have a virtual table pointer that will point to Derived::Fun. This will increase the time to construct a Derived instance by approximately the amount of time that it takes light to travel from your screen to your eye, so I wouldn't worry about it too much :)
If the signature of a function in the derived class matches the signature of a virtual function in the base class the function in the derived class becomes automatically virtual too. I.e. The pointer to the function of the derived class will replace the function pointer of the base class in the virtual function table.

This is done in the constructor by the way. That's the reason why a base class cannot call virtual functions of the derived class whithin its constructor.
Thanks All for replay.

From above comments i am cleared that if Base class function is virtual then same override function in Derived class became virtual automatically. And Virtual table of each class have function pointer pointing their own function.

Now I am not getting how run time resolved below line using this Virtual table and virtual pointer.

ptr->Fun(); // Derived Fun() will get call

Please explain.




Now I am not getting how run time resolved below line using this Virtual table and virtual pointer.

It's implementation dependent, but one way to do it is with an extra hidden member of class Base. This hidden member points to a table of function pointers (one for each virtual function in class Base). Any time a Base object (or a derived object) calls Fun, the compiler generates code to
- dereference the hidden member to point to the table.
- Get the appropriate function pointer in the table
- Call the function.

Now when you declare a derived class, the compiler generates a new table of virtual functions for class Derived. If Derived overrides one of Base's virtual functions, then the corresponding entry in the table points to the new function. If Derived defines new virtual functions, then they go at the end of the table.

Base* ptr = new Derived();
This creates a Derived instance, which means that it's vtab points to the function table for class Derived. The entry in the table for Fun() points to Derived::Fun().
ptr->Fun();
Even though ptr is of type Base, the vtab pointer in *ptr points to the function table for class Derived. So when you lookup the function, you find Derived::Fun(), not Base::Fun()

BTW, as you can see, this means that calling a virtual function is slightly more complicated than calling a function directly. Calling the virtual function means doing 2 memory references. Sometimes the compiler can figure out which one is being called and optimize the code, but in general it's more expensive.
Thanks for details.

but as you said:
Base* ptr = new Derived();
If above line creates the Derived instance, Then why its not called directly the derived class Fun(). why its needed virtual table and all to resolve Fun() call?

Is it because to resolve the ambiguity between the Base::Fun() and Derived::Fun(), As Derived class have both?

Please explain... :)


It's because ptr is a Base* so it could point to a Base object or it could point to a Derived object. The virtual table is needed to lookup the correct Fun() function to be called.
Last edited on
but as you said:
Base* ptr = new Derived();
If above line creates the Derived instance, Then why its not called directly the derived class Fun(). why its needed virtual table and all to resolve Fun() call?

Because it's not always clear to the compiler what type ptr points to. When it is clear, the compiler is free to call that function directly.

But, in a more realistic situation, which should we call?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdlib>
#include <ctime>

class Base
{
    int a;
public:
    virtual void Fun(){}
};

class Derived : public Base
{
     int b;
public:
    void Fun(){}
};

int main()
{
    std::srand(std::time(0));
    Base* ptr = (std::rand()%2 ?  new Derived() : new Base()) ;
    ptr->Fun(); // Which gets called?
} 
Topic archived. No new replies allowed.