问题描述
每个包含一个或多个虚函数的类都有一个与它相关的V表。一个名为vptr的void指针指向该vtable。该类的每个对象都包含指向同一个Vtable的vptr。那么为什么vptr不是静态的呢?而不是将vptr与对象关联,为什么不将它与类关联?
Every class which contains one or more virtual function has a Vtable associated with it. A void pointer called vptr points to that vtable. Every object of that class contains that vptr which points to the same Vtable. Then why isn't vptr static ? Instead of associating the vptr with the object, why not associate it with the class ?
推荐答案
对象的运行时类是对象本身的属性。实际上, vptr
表示运行时类,因此不能是 static
。但是,它指向的是同一个运行时类的所有实例可以共享的。
The runtime class of the object is a property of the object itself. In effect, vptr
represents the runtime class, and therefore can't be static
. What it points to, however, can be shared by all instances of the same runtime class.
这篇关于为什么vptr不是静态的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!