z***e 发帖数: 5393 | 1 Is there any way to get vptr or check the size of virtual function table?
thx. |
c****e 发帖数: 1453 | 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 发帖数: 6806 | 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 发帖数: 2050 | 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 发帖数: 119 | 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 发帖数: 6806 | 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
|
c********x 发帖数: 84 | 7
http://en.wikipedia.org/wiki/Virtual_table
【在 z***e 的大作中提到】 : Is there any way to get vptr or check the size of virtual function table? : thx.
|
c*****g 发帖数: 119 | 8 2.是说:程序运行的时候没法知道到底一个object有几个virtual functions。如果
class里virtual function改变了,其他的code相应地都得变。
the
【在 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.
|
c*****g 发帖数: 119 | 9 原来有typo,cannot中not漏了。
【在 c*****g 的大作中提到】 : 2.是说:程序运行的时候没法知道到底一个object有几个virtual functions。如果 : class里virtual function改变了,其他的code相应地都得变。 : : the
|
p***o 发帖数: 1252 | 10 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?
【在 c*****g 的大作中提到】 : 2.是说:程序运行的时候没法知道到底一个object有几个virtual functions。如果 : class里virtual function改变了,其他的code相应地都得变。 : : the
|
W*********g 发帖数: 409 | 11 不可行。首先vptr在哪不清楚,微软是放在前面,但是更直接的方法是在后面,如果是
这样,对于derived class来说vptr就在中间了,这还没有考虑multiple inherit(多
个vtbl) 和virtual inherit(virtual base class有自己的vtbl)。
其次vtbl里还有其他东西,比如typeid。有些实现会把this pointer adjustment
offsets放在vtbl里。 |