d********t 发帖数: 9628 | 1 VTable是一个class一个,VPtr是每个object一个对吧? |
l*****a 发帖数: 14598 | 2 0
if there is no Virtual function
【在 d********t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : VTable是一个class一个,VPtr是每个object一个对吧?
|
r****t 发帖数: 10904 | 3 明显不是的阿。
【在 d********t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : VTable是一个class一个,VPtr是每个object一个对吧?
|
a********m 发帖数: 15480 | 4 是。
【在 d********t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : VTable是一个class一个,VPtr是每个object一个对吧?
|
s******n 发帖数: 3946 | |
d********t 发帖数: 9628 | 6 大侠不用C++的吧。Java里面可能不讲这个。
【在 s******n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 什么是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 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 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 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 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 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 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 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 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 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 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
|