Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how c++ implements the polymorphism internally?

Tags:

c++

oop

Respected Sir!

i should tell you that what i know and what i don't know about the asked question so that you can address the weak area of my understanding.

i know that c++ implements the polymorphism by using the Vtable which is array of pointers each pointer points to the virtual function of the class, each class in the hierarchy has a vtable. now suppose i have the following class

class person
{
    char name[20];
public:
    person(char* pname)
    {
        strcpy(name,pname);
    }

    virtual void show()
    {
        cout<<"inside person show method, Name: "<<name;
    }
};

class teacher:public person
{
     int scale;

     teacher(char*pname, int s):person(pname)
     {
         scale=s;
     }

     void show()
     {
         cout<<"inside the teacher show method, Scale: "<<scale;
     }
};

now suppose i write in main program

person *ptr;
ptr=new teacher(16,"Zia");
ptr->show();

now i am confuse at this point, the call will go to the show function of the base class, now as it is a virtual function so it inturn calls the approprite function. i know i am wrong here. i am confused that what would be the sequence of calls. What is the role of Vtable and how it works please elaborate.

like image 949
Zia ur Rahman Avatar asked Jan 31 '10 08:01

Zia ur Rahman


People also ask

How is polymorphism implemented in C?

Master C and Embedded C Programming- Learn as you go Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance. C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.

Can C show polymorphism?

Master C and Embedded C Programming- Learn as you goPolymorphism is a key feature of object oriented programming that means having multiple forms. This is divided into compile time polymorphism and runtime polymorphism in C++. An example of compile time polymorphism is function overloading or operator overloading.

What is runtime polymorphism how they are implemented?

Runtime polymorphism in Java is also popularly known as Dynamic Binding or Dynamic Method Dispatch. In this process, the call to an overridden method is resolved dynamically at runtime rather than at compile-time. You can achieve Runtime polymorphism via Method Overriding.

How polymorphism can be implemented using function overloading?

You can implement compile-time polymorphism using function overloading and operator overloading. Method/function overloading is an implementation of compile-time polymorphism where the same name can be assigned to more than one method or function, having different arguments or signatures and different return types.


2 Answers

Since show is declared virtual in the person class, the compiler will not hard-code the method call like it would do for a non-virtual method, it will instead compile a lookup in the V-table in order to retrieve the right function.

So ptr->show() will be compiled as ptr->vtable['show']() which means "search the function pointer that corresponds to method show and execute it".

Since at runtime, ptr points to an object of class teacher, the vtable slot for show contains a pointer to the method show in the class teacher. That is why the right method is executed.

Actually, lookup in the V-table is not done using strings but using numeric method identifiers in order to be as fast as possible.

like image 177
Vincent Robert Avatar answered Oct 04 '22 12:10

Vincent Robert


I think you should draw attention to Stanley B. Lippman's book "Inside C++ object model".

Lets look for internal presentation for your classes:

Virtual Table for person and teacher

|---------------|  +---> |------------------------|
|  name         |  |     | "type_info" for person |
|---------------|  |     |------------------------|
|__vptr__person |--+     | "person::~person"      |
|---------------|        |------------------------|
person p;                | "person::show"         |
                         |------------------------|

|----------------|  +---> |-------------------------|
|person subobject|  |     | "type_info" for teacher |
|----------------|  |     |-------------------------|
|__vptr__teacher |--+     | "teacher::~teacher"     |
|----------------|        |-------------------------|
teacher t;                | "teacher::show"         |
                          |-------------------------|               

In general, we don't know the exact type of the object ptr addresses at each invocation of show(). We do know, however, that through ptr we can access the virtual table associated with the object's class.

Although we don't know which instance of show() to invoke, we know that each instance's address is contained in slot 2.

This information allows the compiler to internally transform the call into

( *ptr->vptr[ 2 ] )( ptr ); 

In this transformation, vptr represents the internally generated virtual table pointer inserted within each class object and 2 represents show()'s assigned slot within the virtual table associated with the Point hierarchy. The only thing we need to do in runtime is compute ptr's dynamic type (and appropriate vtable) using RTTI.

like image 27
Sergey Teplyakov Avatar answered Oct 04 '22 11:10

Sergey Teplyakov