In a class hierarchy where A is derived from B, and C is derived from A, all having virtual functions, A and C would share the same VPTR as they are in the same class hierarchy. Therefore, the size of C would be the size of a single VPTR, which is typically 8 bytes on a 64-bit platform.
On the other hand, when a class C is derived from two parent classes A and B, and both A and B have a virtual function, C would have two VPTRs - one for each class hierarchy. Therefore, the size of C would be the sum of the sizes of the VPTRs for A and B, which is typically 16 bytes on a 64-bit platform.
虚拟函数与VPTR在类层次结构中的影响
文章讨论了在不同类层次结构中,当类A从B派生且C从A派生时,共享VPTR导致C的大小;反之,如果C从两个基类A和B派生,每个基类都有虚拟函数,则C将有两个VPTR,总大小是两个VPTR的和。
458

被折叠的 条评论
为什么被折叠?



