“这个”之间的关系指针和虚函数表函数
我对虚拟表的功能不太了解,但在下面粘贴的代码中 - 传递的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
A
根本没有 vtable(或者在任何好的编译器上都不应该有),因为它不是一个多态类:它没有虚拟成员函数。函数
show()
在可执行文件中仅存在一次。成员函数与普通非成员函数并没有真正的区别,它们只是有一个额外的隐式this
参数。您可以将其视为编译器将成员函数转换为类似的非成员函数,例如:调用此非成员函数的类似方法是使用 <代码>显示(&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, implicitthis
parameter. You can think of it as if the compiler transforms a member function into a similar nonmember function, for example:Instead of
OBJ_1.show()
, the comparable way to call this nonmember function would be to useshow(&OBJ_1)
.There isn't one
A::show()
perA
object that is created. There's oneA::show()
total and it takes as an argument the instance on which it was called.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
不,每个函数(成员或非成员)对应的代码只有一份。该函数使用
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.