P********e 发帖数: 2610 | 1 class A
{
int i;
public:
virtual void f(){}
};
class DerivedA: public A
{
int j;
public:
virtual void f(){}
virtual void ff(){}
};
DerivedA da;
为什么da里面没有一个vptr是DerivedA?
da 的layout是 :
[
i
A.vptr
j
// 为什么这里没有DerivedA.vptr???
] |
P********e 发帖数: 2610 | 2 我大概知道为什么没有额外的vptr in da
不过这样的情况,是怎么处理的?
A a = da;
因为, a 和 da里面 vptr地址是不一样的.这样怎么能保证integrity of subobject in
da.
【在 P********e 的大作中提到】 : class A : { : int i; : public: : virtual void f(){} : }; : class DerivedA: public A : { : int j; : public:
|
X****r 发帖数: 3557 | 3 我先问你A a = da;这句是什么?
in
【在 P********e 的大作中提到】 : 我大概知道为什么没有额外的vptr in da : 不过这样的情况,是怎么处理的? : A a = da; : 因为, a 和 da里面 vptr地址是不一样的.这样怎么能保证integrity of subobject in : da.
|
P********e 发帖数: 2610 | 4 继承object转换到父类,来检验da有没有完整的父类subobject
【在 X****r 的大作中提到】 : 我先问你A a = da;这句是什么? : : in
|
X****r 发帖数: 3557 | 5 For single non-virtual inheritence like this, da's vptr points
to DeriveA's vtable, which is compatible with A's vtable.
【在 P********e 的大作中提到】 : class A : { : int i; : public: : virtual void f(){} : }; : class DerivedA: public A : { : int j; : public:
|
P********e 发帖数: 2610 | 6 i'm not sure what do you mean by compatible?
【在 X****r 的大作中提到】 : For single non-virtual inheritence like this, da's vptr points : to DeriveA's vtable, which is compatible with A's vtable.
|
X****r 发帖数: 3557 | 7 For every virtual function of A, its position in A's vtable
is the same as the position of the same or overridden function
of DerivedA in DerivedA's vtable.
【在 P********e 的大作中提到】 : i'm not sure what do you mean by compatible?
|
P********e 发帖数: 2610 | 8 谢谢
那这个问题还是不太清楚:
A a = da;
上面这个怎么保证,da里面的subobject的完整性,下面这样理解对吗?
compiler要做:
a.i = da.i;
a.vptr = A:vptr;???这样吗?
For every virtual function of A, its position in A's vtable
is the same as the position of the same or overridden function
of DerivedA in DerivedA's vtable.
【在 X****r 的大作中提到】 : For every virtual function of A, its position in A's vtable : is the same as the position of the same or overridden function : of DerivedA in DerivedA's vtable.
|
P********e 发帖数: 2610 | 9 如果base declare any virtual function, compiler will synthesize a copy ctor
to do it.
now it's clear. thanks x.
【在 P********e 的大作中提到】 : 谢谢 : 那这个问题还是不太清楚: : A a = da; : 上面这个怎么保证,da里面的subobject的完整性,下面这样理解对吗? : compiler要做: : a.i = da.i; : a.vptr = A:vptr;???这样吗? : : For every virtual function of A, its position in A's vtable : is the same as the position of the same or overridden function
|
X****r 发帖数: 3557 | 10 对。
A a = da;就是调用隐含定义的A::A(const A& other),this为&a,
other为da里的a子对象。
这个copy constructor和A的其他所有constructor一样,把this的vptr指向
A的vtable。
【在 P********e 的大作中提到】 : 谢谢 : 那这个问题还是不太清楚: : A a = da; : 上面这个怎么保证,da里面的subobject的完整性,下面这样理解对吗? : compiler要做: : a.i = da.i; : a.vptr = A:vptr;???这样吗? : : For every virtual function of A, its position in A's vtable : is the same as the position of the same or overridden function
|
P********e 发帖数: 2610 | 11 我开始没把他们联系起来,觉得子类的vptr好像不对。。。
【在 X****r 的大作中提到】 : 对。 : A a = da;就是调用隐含定义的A::A(const A& other),this为&a, : other为da里的a子对象。 : 这个copy constructor和A的其他所有constructor一样,把this的vptr指向 : A的vtable。
|