由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 为什么derived object没有vptr?
相关主题
C++小插曲C++ 弱问一个
A C++ compiler related interview question请问c++中操作符可以声明为虚函数吗?
C++ Q98: Call member function in virtual function (转载)C++问题
C++问题几个c++ dynamic cast
How to check the virtual function table size?some c++ question.
问题:vptr/vtable for virtual function & vptr/vtable forC++虚函数动态绑定的含义?
师傅们都出来看看吧,我也问个C++返回值问题。C++ virtual function 定义在 derived class 会怎么样?
我有个很傻的问题,关于function call via pointerOverridden function will cause function shadow in C++, but not in Java
相关话题的讨论汇总
话题: deriveda话题: da话题: vptr话题: virtual话题: vtable
进入Programming版参与讨论
1 (共1页)
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。

1 (共1页)
进入Programming版参与讨论
相关主题
Overridden function will cause function shadow in C++, but not in JavaHow to check the virtual function table size?
为啥 const Base cb 要求Base() {} 而 const vBase vb 不呢?问题:vptr/vtable for virtual function & vptr/vtable for
问个虚函数的作用师傅们都出来看看吧,我也问个C++返回值问题。
一个初级问题,查了很久没有结果,请大侠指点迷津我有个很傻的问题,关于function call via pointer
C++小插曲C++ 弱问一个
A C++ compiler related interview question请问c++中操作符可以声明为虚函数吗?
C++ Q98: Call member function in virtual function (转载)C++问题
C++问题几个c++ dynamic cast
相关话题的讨论汇总
话题: deriveda话题: da话题: vptr话题: virtual话题: vtable