“这个”之间的关系指针和虚函数表函数

发布于 2024-10-25 03:08:10 字数 664 浏览 1 评论 0原文

我对虚拟表的功能不太了解,但在下面粘贴的代码中 - 传递的 this 指针显然指向两种情况中的不同位置 - 但函数 show () 在内存中 - 它是否在运行时为每个对象单独实例化/创建? (请原谅我对 C++ 术语的理解很差)

#include<iostream>
using namespace std;
class A
{
      int x;
      public:
            A(){x=0;}
            A(int z){x=z;}
            void show()
            {  
                 if(x==0)
                 cout<<"\nCalled by OBJ_1";
                 else
                 cout << "\nCalled by OBJ_2";
            }
};

int main()
{
 A OBJ_1,OBJ_2(1);
 OBJ_1.show();
 OBJ_2.show();
}

如果可以提供一个关于虚拟表如何工作的示例(如果可能的话,带有一些内存图)以及可以解释虚拟表的 this 指针的功能,我将非常感激。

I do not have much idea about the functionality of the Virtual Table, but in the code pasted below - the this pointer passed is obviously points to different locations in the 2 cases - but the function show() in the memory - is it instantiated/meaning created separately for every object at Runtime? (Forgive my poor understanding of C++ jargons)

#include<iostream>
using namespace std;
class A
{
      int x;
      public:
            A(){x=0;}
            A(int z){x=z;}
            void show()
            {  
                 if(x==0)
                 cout<<"\nCalled by OBJ_1";
                 else
                 cout << "\nCalled by OBJ_2";
            }
};

int main()
{
 A OBJ_1,OBJ_2(1);
 OBJ_1.show();
 OBJ_2.show();
}

If an example (with some memory-diagrams if possible) could be provided regarding how the Virtual Table works and the functionality of the this pointer w.r.t Virtual Table could be explained, i would much appreciate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生活了然无味 2024-11-01 03:08:10

A 根本没有 vtable(或者在任何好的编译器上都不应该有),因为它不是一个多态类:它没有虚拟成员函数。

函数 show() 在可执行文件中仅存在一次。成员函数与普通非成员函数并没有真正的区别,它们只是有一个额外的隐式 this 参数。您可以将其视为编译器将成员函数转换为类似的非成员函数,例如:

void show(A* this)
{
    if (this->x == 0)
        cout << "\nCalled by OBJ_1";
    else    
        cout << "\nCalled by OBJ_2";  
}

调用此非成员函数的类似方法是使用 <代码>显示(&OBJ_1)。

创建的每个 A 对象没有一个 A::show()。总共有一个 A::show() ,它以调用它的实例作为参数。

A does not have a vtable at all (or it shouldn't on any good compiler) because it is not a polymorphic class: it has no virtual member functions.

The function show() exists in the executable exactly once. Member functions aren't really different from ordinary nonmember functions, they just have an extra, implicit this parameter. You can think of it as if the compiler transforms a member function into a similar nonmember function, for example:

void show(A* this)
{
    if (this->x == 0)
        cout << "\nCalled by OBJ_1";
    else    
        cout << "\nCalled by OBJ_2";  
}

Instead of OBJ_1.show(), the comparable way to call this nonmember function would be to use show(&OBJ_1).

There isn't one A::show() per A object that is created. There's one A::show() total and it takes as an argument the instance on which it was called.

夜未央樱花落 2024-11-01 03:08:10

C++ 标准没有明确定义术语“虚拟表”。实现可以自由地以任何方式实现多态类(至少具有一个虚函数的类)。但最常见的实现使用带有 v-ptr 的 v-table。

看看 Marshall Cline 关于虚拟函数的说法

C++ standard doesn't define the term "virtual table" explicitly. An implementation is free to implement a polymorphic class (a class which has at least one virtual function) in any way it wants. But most common implementations use a v-table with v-ptr.

Check out what Marshall Cline has to say about virtual functions

妳是的陽光 2024-11-01 03:08:10

不,每个函数(成员或非成员)对应的代码只有一份。该函数使用 this 指针来确定实际操作哪个对象。

您没有任何virtual方法,因此您的代码中根本没有vtable。

No, there is only one copy of the code corresponding to each function (member or non-member). The function uses the this pointer to determine which object to actually operate on.

You don't have any virtual methods so there's no vtable in your code at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文