c ++如何在内部实现多态? [英] how c++ implements the polymorphism internally?

查看:166
本文介绍了c ++如何在内部实现多态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的先生!

我应该告诉你,我知道我不知道的问题,所以你可以解决我的弱点理解。

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.

我知道c ++通过使用指针数组的Vtable实现多态性
每个指针指向类的虚函数,每个类在层次结构中有一个vtable。现在假设我有以下类

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();



现在我很困惑,此时,调用将转到基类的show函数,现在因为它是一个虚函数所以它内部调用的approprite函数。我知道我错了。我很困惑,什么是呼叫的顺序。

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.

推荐答案

我想你应该注意Stanley B. Lippman的书 C ++内部对象模型

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

我们为您的课程寻找内部简报:

Lets look for internal presentation for your classes:

人与老师的虚拟表

|---------------|  +---> |------------------------|
|  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"         |
                          |-------------------------|               

一般来说,在每次调用show时,我们不知道对象ptr地址的确切类型()。然而,我们知道,通过ptr,我们可以访问与该对象的类相关联的虚拟表。

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.

虽然我们不知道调用show()的哪个实例,我们知道每个实例的地址包含在插槽2中。

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 ); 

在此转换中,vptr表示在每个类对象内插入的内部生成的虚拟表指针,2表示show ()在与点层次结构相关联的虚拟表中的分配时隙。我们在运行时需要做的唯一的事情是计算ptr的动态类型(和适当的vtable)使用RTTI。

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.

这篇关于c ++如何在内部实现多态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆