问题描述
Hello All,
我在很多c ++文献中都读到vtable只不过是一个数组
指向类中的虚函数的指针。而且这个类在哪里
虚拟函数被声明存储为vfptr,即内部指向
vtable的指针。
令我困惑的是
如果vtable是一个指向虚函数的指针数组,那么根据数组的所有条目,数组中的所有条目必须是
same.ie所有指针应该是类似的类型。这将对虚拟函数的声明施加
限制,因为它们必须
具有与我的观点相同的单一性。
例如。 class base
{
public:
virtual void fun(int,int);
virutal int fun2( char);
};
现在根据vtable的定义(即指向
虚拟函数的指针数组)
数组的语法是什么?因为单个数组不能保持
具有不同数据类型的条目。
指向第一个虚函数的指针就像
void(base: :* fptr1)(int,int)和第二个类似
int(base :: * fptr2)(char)
如何存储像vtable这样的单个数组这两个完全不同的b
但是一个结构可以存储多种数据类型。
所以Vtable是一个结构或一个数组?如果它的阵列那么任何人都可以给它定义它的价值吗?
谢谢和问候,
Yogesh Joshi
[见有关的信息]
[comp.lang.c ++。版主。第一次海报:做到这一点! ]
Hello All,
I have read in many c++ literature that vtable is nothing but an array
of pointer to virtual functions inside a class.And the class where the
virtual function/s are declared stores the vfptr i.e the pointer to
vtable internally.
What confuses me is
if vtable is an array of pointer to virtual functions then as per the
properties of the array all the entries inside the array must be
same.i.e all the pointers should be of similar type.and this will put
restriction on the declaration of the virtual functions as they must
have same singnature as per my point.
eg. class base
{
public:
virtual void fun(int,int);
virutal int fun2(char);
};
now according to the definition of vtable(i.e array of pointers to
virtual functions)
what will be the syntax for the array? as single array can''t hold
entries with different datatypes.
the pointer to first virtual function is something like
void (base::*fptr1)(int,int) and for second its like
int (base::*fptr2)(char)
how come a single array like vtable will stored these two totally
different entires?
but a structure can stores mulitple datatypes.
So is Vtable a structure or an array? if its array then can anyone give
me its definition?
Thanks and regards,
Yogesh Joshi
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
推荐答案
1. C ++ std没有说明实现虚拟的内容
函数。编译器可以通过任何(正确的)
机制自由实现它。 (但是,今天存在的几乎大部分(或者它是'all''?)
编译器使用vtable
机制来实现虚函数。)
2.当你想写一个C ++代码来获得
类型时,你所讨论的类型限制对于一个
程序员来说是存在的 - 在编译期间检查。
编译器不存在此类限制。具体来说,编译器不生成对应于函数指针数组的C ++代码
- 它只是将与索引相对应的
汇编代码吐出到一个addessses数组中。所以
类型检查的问题真的出现在图片中。
当然,我还没有真正看到实施,所以我可能不会
准确无误。欢迎提出意见。
1. C++ std doesnot say anything about implementation of virtual
functions. The compilers are free to implement it by any (correct)
mechanism. (However, practically majority of the (or is it ''all'' ? )
compilers that exist today implement virtual functions using vtable
mechanism.)
2. The type restrictions that you are talking about exist for a
programmer when he wants to write a C++ code which will get
type-checked during compilation. Such restrictions donot exist for the
compiler. Specifically, the compiler doesnot generate a C++ code
corresponding to array of function pointers - it simply spits out the
assembly code corresponding to indexing into an array of addessses. So
the issues of type-checking donot really come in the picture.
Of couse, I have not really seen the implemenation and so I might not
be accurate. Comments are welcome.
v-table超出了C ++。你不能直接访问它,或者获得一个指向v表的
指针。
编译器的编写者可能会像它们一样实现它br />
实现了一个函数指针数组和那些
表示可能针对编译器编写器的C ++文献,或者可能是
说明如何一个人可以模拟一个v-table(例如,如果用另一种语言编写,如b的b b语言)。
[见有关的信息]
[comp.lang.c ++。审核。第一次海报:做到这一点! ]
The v-table is beyond C++. You cannot access it directly, or get a
pointer to a v-table.
The writers of the compiler may well implement it the same way they
implement an array of function pointers and the C++ literature that
indicates as such may either be aimed at compiler-writers or may be
illustrating how one could simulate a v-table (eg if coding in another
language such as C).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
我觉得你很困惑,因为你试图继续/>
关于编译器实现领域的C ++语言的规则
和运行时体系结构。 vtable是记忆中的一个区域。如果你愿意,可以把它称为
数组。 C ++并不允许你直接分配异构数组
,但如果你真的想要,你可以随时解决这个问题。
只需分配一块内存,管理你的指针非常仔细,如果你得到的话,你可以投资
,你真的完全掌控了。但是你在
这个案例是编译器实现者,他甚至可能使用或不使用
C ++。
重点是,它是实现定义的。所需要的只是
提供一些支持虚拟化行为的机制
标准中要求的
。除非你有理由深入研究编译器内部或
之类的东西,否则这就是你应该依赖的所有东西,或者担心
。
卢克
[见了解有关的信息]
[comp.lang.c ++。moderated。第一次海报:做到这一点! ]
I think you''re getting confused because you''re trying to carry over
rules about the C++ language to the domain of compiler implementation
and runtime architecture. The vtable is an area in memory. Call it an
array if you want. C++ doesn''t let you allocate heterogeneous arrays
directly, but you can always get around this if you really want to.
Just allocate a chunk of memory, manage your pointers very carefully,
cast if ya gotta, and you really have complete control. But "you" in
this case is the compiler implementor, who may or may not even be using
C++.
The point is, it''s implementation-defined. All that''s required is that
some mechanism be provided that supports the virtualization behaviour
required in the standard. And that''s all you should count on, or worry
about, unless you have a reason to delve into compiler internals or the
like.
Luke
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
这篇关于什么是vtable?它是一个结构还是一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!