由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 我有个很傻的问题,关于function call via pointer
相关主题
C++小插曲C++虚函数动态绑定的含义?
A C++ compiler related interview question*** help needed! on MATLAB GUI ***
some c++ question.A C++ puzzle for me
How to check the virtual function table size?Question about data member offset
问题:vptr/vtable for virtual function & vptr/vtable for再问C++问题。
为什么derived object没有vptr?问一个C++问题:default parameter and overriding/inheritanc (转载)
问个虚函数的作用c++里的函数可不可以是virtual+static
师傅们都出来看看吧,我也问个C++返回值问题。Two questions on virtual destructor
相关话题的讨论汇总
话题: virtual话题: vptr话题: runtime话题: pointer话题: void
进入Programming版参与讨论
1 (共1页)
P********e
发帖数: 2610
1
suppose we have:
class A
{
public:
virtual ~A(){}
void x(){}
virtual void y(){}
};
A * a = new A();
a->x();
a->y();
因为x不在v table里面.
在runtime的时候,linker怎么知道y在vptr,而x不在vtable里面
也就是,他们怎么知道a->y();要转换成 (*a->vptr[1])();
a->x()就不需要呢
m*****e
发帖数: 4193
2

Because of the 'virtual' keyword?

【在 P********e 的大作中提到】
: suppose we have:
: class A
: {
: public:
: virtual ~A(){}
: void x(){}
: virtual void y(){}
: };
: A * a = new A();
: a->x();

r****t
发帖数: 10904
3
这个是 runtime 时候的么?

【在 P********e 的大作中提到】
: suppose we have:
: class A
: {
: public:
: virtual ~A(){}
: void x(){}
: virtual void y(){}
: };
: A * a = new A();
: a->x();

P********e
发帖数: 2610
4
good guess, that the key. but still how?
x is resolved at compile?

【在 m*****e 的大作中提到】
:
: Because of the 'virtual' keyword?

P********e
发帖数: 2610
5
function call via pointer是runtime resolved
比如:
class A
{
virtual x(){}
}
class B : public A
{
x(){}
}
A * a = new B();
a->x();
这个compile的时候不知道what object a addresses.

【在 r****t 的大作中提到】
: 这个是 runtime 时候的么?
t****t
发帖数: 6806
6
你能知道y是virtual, x不是, 为什么编译器不知道?

【在 P********e 的大作中提到】
: suppose we have:
: class A
: {
: public:
: virtual ~A(){}
: void x(){}
: virtual void y(){}
: };
: A * a = new A();
: a->x();

P********e
发帖数: 2610
7
所以你觉得这个是在compile的时候resolve的?

【在 t****t 的大作中提到】
: 你能知道y是virtual, x不是, 为什么编译器不知道?
t****t
发帖数: 6806
8
for ->x(), of course.

【在 P********e 的大作中提到】
: 所以你觉得这个是在compile的时候resolve的?
X****r
发帖数: 3557
9
对于一个成员函数是不是virtual显然是编译时就决定的,
对于已经确定是virtual的成员函数,具体调用那个类的,
(大多数情况下)才是运行时决定的。

【在 P********e 的大作中提到】
: 所以你觉得这个是在compile的时候resolve的?
P********e
发帖数: 2610
10
所以compiler 对 非virtual和virtual的 是不一样处理的。。。
like this?
a->x(); -> x_AV3DSFS( a );
a->y(); -> (*a->vptr[1]) ();

【在 X****r 的大作中提到】
: 对于一个成员函数是不是virtual显然是编译时就决定的,
: 对于已经确定是virtual的成员函数,具体调用那个类的,
: (大多数情况下)才是运行时决定的。

d*********8
发帖数: 2192
11
early binding and late binding
1 (共1页)
进入Programming版参与讨论
相关主题
Two questions on virtual destructor问题:vptr/vtable for virtual function & vptr/vtable for
c++ define 一问为什么derived object没有vptr?
An interesting C++ compile error问个虚函数的作用
问个面试问题,请教师傅们都出来看看吧,我也问个C++返回值问题。
C++小插曲C++虚函数动态绑定的含义?
A C++ compiler related interview question*** help needed! on MATLAB GUI ***
some c++ question.A C++ puzzle for me
How to check the virtual function table size?Question about data member offset
相关话题的讨论汇总
话题: virtual话题: vptr话题: runtime话题: pointer话题: void