由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Virtual pointer是每个object都有一个吗?
相关主题
a virtual table question by JP morgan攒人品 报BB面经
问几个跟C++有关的面试题问个硬件加OO的面试题
问一个c++ virtual base class的问题virtual table存在memory的哪块啊?
C++ Q21: size of virtual tableone C++ question?
现在招个会C++的人真难C++ Q52: (C6)
C++ object size一问virtual destructor (C++)问题
【回报本版】英伟达电面试第一轮+明天第二轮求祝福virtual destructor的地址在virtual table里面吗?
发个pure storage面经,电面跪了微软C++面试题
相关话题的讨论汇总
话题: virtual话题: object话题: vptr话题: func话题: vtable
进入JobHunting版参与讨论
1 (共1页)
d********t
发帖数: 9628
1
VTable是一个class一个,VPtr是每个object一个对吧?
l*****a
发帖数: 14598
2
0
if there is no Virtual function

【在 d********t 的大作中提到】
: VTable是一个class一个,VPtr是每个object一个对吧?
r****t
发帖数: 10904
3
明显不是的阿。

【在 d********t 的大作中提到】
: VTable是一个class一个,VPtr是每个object一个对吧?
a********m
发帖数: 15480
4
是。

【在 d********t 的大作中提到】
: VTable是一个class一个,VPtr是每个object一个对吧?
s******n
发帖数: 3946
5
什么是virtual pointer???
d********t
发帖数: 9628
6
大侠不用C++的吧。Java里面可能不讲这个。

【在 s******n 的大作中提到】
: 什么是virtual pointer???
s******n
发帖数: 3946
7
every object will have a pointer to the vtable for each virtual class.
So if an object inherit two virtual classes, it will have two pointers to
the two virutal classes, if this class added new virtual functions, then
this class itself will have a virtual table, so it will totally have three
pointers to vtable.
s****a
发帖数: 528
8
Wrong, still have only one Vptr to the virtual table

three

【在 s******n 的大作中提到】
: every object will have a pointer to the vtable for each virtual class.
: So if an object inherit two virtual classes, it will have two pointers to
: the two virutal classes, if this class added new virtual functions, then
: this class itself will have a virtual table, so it will totally have three
: pointers to vtable.

s******n
发帖数: 3946
9
you are wrong. Just draw a picture of the memory layout of a object which
inherits two classes with virtual methods, see how many vtable pointers in
the memory of that object.

【在 s****a 的大作中提到】
: Wrong, still have only one Vptr to the virtual table
:
: three

z****u
发帖数: 104
10
why a child class not just maintain one table which includes all the virtual
functions it inherited from base classes and its own?

three

【在 s******n 的大作中提到】
: every object will have a pointer to the vtable for each virtual class.
: So if an object inherit two virtual classes, it will have two pointers to
: the two virutal classes, if this class added new virtual functions, then
: this class itself will have a virtual table, so it will totally have three
: pointers to vtable.

s******n
发帖数: 3946
11
When compiler handles b->func(para1, para2), it assumes the address is like
this
vptr_BClass
B data member 1
B data member 2
The call will be translated like this:
((vptr_BClass*)b)[0] -> func (b, para1, para2)
class C extends B & A, then memory layout is like this:
vptr_AClass
A data member 1
A data member 2
vptr_BClass
B data member 1
B data member 2
When we do B* b= &c, compiler will add offset to skip the A part. So a
later call b->func() can be handled in the same way for all subclasses of B.
If you merged virtual table AClass and BClass, how could compiler handle
this:
// in CPP1:
B* b1 = &c; (assume class C extends A&B)
B* b2 = &d; (assume class D extends A&B)
...
// in CPP2:
b1->func();
b2->func();
b1 and b2 points to different merged virtual tables. How does compiler know
the position of func in virtual table for b1 and the position of func in
virtual table for b2? The are two different merged tables, func is at
different locations in vtable.

virtual

【在 z****u 的大作中提到】
: why a child class not just maintain one table which includes all the virtual
: functions it inherited from base classes and its own?
:
: three

s**x
发帖数: 7506
12
We have two votes, they all point to vtbl of c.
Also, vtbl for virtual inheritance is more complicated.
The following has details.
http://phpcompiler.org/articles/virtualinheritance.html

like

【在 s******n 的大作中提到】
: When compiler handles b->func(para1, para2), it assumes the address is like
: this
: vptr_BClass
: B data member 1
: B data member 2
: The call will be translated like this:
: ((vptr_BClass*)b)[0] -> func (b, para1, para2)
: class C extends B & A, then memory layout is like this:
: vptr_AClass
: A data member 1

1 (共1页)
进入JobHunting版参与讨论
相关主题
微软C++面试题现在招个会C++的人真难
一个简单的java题C++ object size一问
c++ vs Java virtual 实现(Y家)【回报本版】英伟达电面试第一轮+明天第二轮求祝福
问道C的面试题发个pure storage面经,电面跪了
a virtual table question by JP morgan攒人品 报BB面经
问几个跟C++有关的面试题问个硬件加OO的面试题
问一个c++ virtual base class的问题virtual table存在memory的哪块啊?
C++ Q21: size of virtual tableone C++ question?
相关话题的讨论汇总
话题: virtual话题: object话题: vptr话题: func话题: vtable