Is there any way to get vptr or check the size of virtual function table? thx.
c*e
2 楼
Statically, I think you can count the number of virtual functions and times 4. Here is my guess: suppose we want to know the vtable size of class B. build D, class D : public B { virtual void test(){} }; now, in the memory: D: +0: pointer to vtable vtabble: ... v functions in class B test() D * pd= new D; void (*p)()=pd->test; char * vtable_base=(char *)(*(int *)pd); vsize=(char *)p - vtable_base;
t*t
3 楼
看上去很不错,写成template就更牛了
times
【在 c****e 的大作中提到】 : Statically, I think you can count the number of virtual functions and times : 4. : Here is my guess: : suppose we want to know the vtable size of class B. : build D, : class D : public B : { : virtual void test(){} : }; : now, in the memory:
d*d
4 楼
times ~~~~~~~ 有一点不明白的,请问这里是为什么。
【在 c****e 的大作中提到】 : Statically, I think you can count the number of virtual functions and times : 4. : Here is my guess: : suppose we want to know the vtable size of class B. : build D, : class D : public B : { : virtual void test(){} : }; : now, in the memory:
c*g
5 楼
1. the layout of the object and vtable in memory depends on compiler implementation 2. you cannot get # of v functions at runtime 3. times by 4 depends on platforms 4. how about multiple inheritence? .....
times
【在 c****e 的大作中提到】 : Statically, I think you can count the number of virtual functions and times : 4. : Here is my guess: : suppose we want to know the vtable size of class B. : build D, : class D : public B : { : virtual void test(){} : }; : now, in the memory:
t*t
6 楼
this is of course implementation defined... the standard doesn't even say there exists a vtable or vptr. but, 1. layout of the object is implementation defined, but *usually* vptr is the first entry. 2. ??? 3. you can use sizeof(any_pointer_to_function) instead. 4. this is indeed a big problem.
【在 c*****g 的大作中提到】 : 1. the layout of the object and vtable in memory depends on compiler : implementation : 2. you cannot get # of v functions at runtime : 3. times by 4 depends on platforms : 4. how about multiple inheritence? : ..... : : times
【在 t****t 的大作中提到】 : this is of course implementation defined... the standard doesn't even say : there exists a vtable or vptr. : but, : 1. layout of the object is implementation defined, but *usually* vptr is the : first entry. : 2. ??? : 3. you can use sizeof(any_pointer_to_function) instead. : 4. this is indeed a big problem.
Well, it's doable if you can change the code and ask others to follow you, e.g. get a virtual function to allow query such information like COM, or have a compiler giving such information like Java ... Otherwise, it's too hard. Do we have any reflection library for C++ now?