由买买提看人间百态

topics

全部话题 - 话题: vtable
1 2 下页 末页 (共2页)
s*******u
发帖数: 1855
1
【 以下文字转载自 JobHunting 讨论区 】
发信人: siriusliu (天狼), 信区: JobHunting
标 题: 问题:vptr/vtable for virtual function & vptr/vtable for virtual inheritance, are they the same?
发信站: BBS 未名空间站 (Wed Nov 5 00:58:56 2008)
we know that both virtual function and virtual inheritance have vptr
pointing to vtable. My question is: do they use one SAME table/ptr, or TWO separate table/ptr?
For ex:
s*******u
发帖数: 1855
2
还有,当一个class有vptr指向vtable时,他的derived class,继承他的member时,是
否继承这个vptr?
感觉应该不继承,但是如果这样,无论如何算不出为什么上个例子中,sizeof(MI) can
be 36.
thanks!
j*p
发帖数: 115
3
来自主题: JobHunting版 - 请问这是什么level的面试题
麻烦你在解释一下吧 还是不懂 还是这个例子
class Base
{
public:
virtual void v();
};
class Derived : public Base
{
public:
void v();
};
void main()
{
Drived D;
Derived *pd = &D;
Base* pb = dynamic_cast(pd);
}
我如果用 (*pb)::v(),这个是调用Base::v() 还是Derived::v()? 应该是Base::v()吧
。但是如果这样的话,pb就不能指向D,因为D里面指向vtable的指针是指向Derived类的
vtable的。所以,pb 应该指向一个D的一个复制,这个复制中的指针指向Base类的
vtable?
我知道vtable是不变的 但是指向vtable的指针在cast之后变不变啊?
j*p
发帖数: 115
4
来自主题: JobHunting版 - 请问这是什么level的面试题
麻烦你在解释一下吧 还是不懂 还是这个例子
class Base
{
public:
virtual void v();
};
class Derived : public Base
{
public:
void v();
};
void main()
{
Drived D;
Derived *pd = &D;
Base* pb = dynamic_cast(pd);
}
我如果用 (*pb)::v(),这个是调用Base::v() 还是Derived::v()? 应该是Base::v()吧
。但是如果这样的话,pb就不能指向D,因为D里面指向vtable的指针是指向Derived类的
vtable的。所以,pb 应该指向一个D的一个复制,这个复制中的指针指向Base类的
vtable?
我知道vtable是不变的 但是指向vtable的指针在cast之后变不变啊?
S**Y
发帖数: 136
5
来自主题: JobHunting版 - 问几个跟C++有关的面试题
let's discuss this..
about vptr and vtable
Since every class has only one copy of vptr and vtable, the vptr and vtable
should be static? ->data area?
How is vptr and vtable implemented indeed?

base;
,
f********a
发帖数: 165
6
来自主题: JobHunting版 - c++ vs Java virtual 实现(Y家)
多谢。是个老毛子,边问边喝伏特加,面完喝完,最后瓶子都留在桌子上。也怪自己学
艺不精。
你的意思是说c++里面最后调用完A的constructor,A的vtable和b的vtable都没建立好,
所以只能用A自己的virtual function?
java里面也是需要建立vtable的?那有没有vpointer一说?为什么在没有执行的时候类
似vtable的东西都已经建好?

用B
的。
g*******y
发帖数: 1930
7
来自主题: JobHunting版 - 问几个跟C++有关的面试题
我来挣点包子~

when delete a base pointer that actually points to a derived obj;
a destructor should be made virtual if that class intends to serve as a base;
compiler generate a virtual table(addresses of VFs) for each class that
contains VF; it secretly put the VPtr(address of Vtable) in the object.
When you call VF through ptr/ref, compiler first get the value of VPtr to
access Vtable, then it can get the address of the function from the Vtable,
because the table offset of the VF is known.
候: 决定的?
n*c
发帖数: 228
8
来自主题: Programming版 - C++问题几个
ctor 的定义里边能用runnable 做参数吗?加入你在写一个库,根本没有class继承
runnable,也就没有instance可以pass.
vtable,interface有vtable吗?其实这才是我关心的问题。abstract class 应该是有
vtable,但是interface呢。如果有的话,implement interface的instance 里应该有一
个vptr才对,但是这样的话interface和abstract class 有什么区别?
java里边是metadata,
那.net的IL呢。
d****p
发帖数: 685
9
来自主题: Programming版 - 码工试题 (转载)

. If it is pure
virtual you'll have compiler error anyways.
Suppose IncA() is virtual.
Compiler normally constructs Bar's vtable in Bar's constructor body; so in
the initialization list this->IncA()
will look for B's vtable which is unfortunately not ready.
Anyway I guess at that time Foo's vtable may be ready and the form (static_
castthis)->IncA() may work.
c****s
发帖数: 241
10
来自主题: JobHunting版 - 面试题
2. 一般是在base class里隐含一个vtable,这个vtable会在调完constructor后初始化
。所以constructor里面不能调用virtual functions
5. 很多是由于死循环或者busy wait,所以看看log file,可能发现那些函数进去了,
但是出不来;另外还可以生成crash dump;还有什么可能方法?
l*****a
发帖数: 14598
11
来自主题: JobHunting版 - 问一个c++ virtual base class的问题
每个类有自己的Vtable,然后constructor会生成自己的Vptr指向自己的Vtable。
v***n
发帖数: 5085
12
题目主要有
哲学
编译
C++
数据结构
算法
编程(C)
第一题就是哲学
问为什么要OO,OO和实际世界之间的关系
然后改问class, object, instance之间的联系与区别
第二题问编译
几个文件里都有static的变量那么哪个先哪个后被编译
第三题问C++
vtable以及在有继承时候的vtable以及vptr
第四题问数据结构
stl里面的set vector list
实现方法 优缺点 有效存储空间比
第五题算法
问基于以上几个ds的一些搜索 删除 插入的时间复杂度
第六题编程
编写一个linked list测节点数量的小程序
j*p
发帖数: 115
13
来自主题: JobHunting版 - 请问这是什么level的面试题
刚才我想了一下,当时是说vtable的时候引出来的 他问的意思应该是 在进行类型转换
之后vtable怎么变化 比如
class Base
{
public:
virtual void v();
}
class Derived : public Base
{
public:
void v();
}
void main()
{
Drived D;
Derived *pd = &D;
Base* pb = dynamic_cast(pd);
}
本来pd指向D,D里面有一个指向Derived类的虚函数表的指针。现在被转成Base类的指针
pb了,pb还是指向D吗?印度人当时问 那个指针是不变呢还是另外copy什么什么。。。
如果是
Base B;
Base *pb = &B;
Derived* pd = dynamic_cast(pb);
pd指向B,还是要复制 B, 然后 在复制后的object里把虚函数表的指针改过来呢?
sigh~ 基本上没用过cast。。。这种题怎么复习得到嘛。。。
a********m
发帖数: 15480
14
来自主题: JobHunting版 - 请问这是什么level的面试题
vtable当然不变,不然怎么知道如何调用正确的虚函数。而且所有同类对象都用同一个
vtable。
这个是c++基本概念,应该搞清楚的。不是是否经常用cast的问题。
j*p
发帖数: 115
15
来自主题: JobHunting版 - 请问这是什么level的面试题
刚才我想了一下,当时是说vtable的时候引出来的 他问的意思应该是 在进行类型转换
之后vtable怎么变化 比如
class Base
{
public:
virtual void v();
}
class Derived : public Base
{
public:
void v();
}
void main()
{
Drived D;
Derived *pd = &D;
Base* pb = dynamic_cast(pd);
}
本来pd指向D,D里面有一个指向Derived类的虚函数表的指针。现在被转成Base类的指针
pb了,pb还是指向D吗?印度人当时问 那个指针是不变呢还是另外copy什么什么。。。
如果是
Base B;
Base *pb = &B;
Derived* pd = dynamic_cast(pb);
pd指向B,还是要复制 B, 然后 在复制后的object里把虚函数表的指针改过来呢?
sigh~ 基本上没用过cast。。。这种题怎么复习得到嘛。。。
a********m
发帖数: 15480
16
来自主题: JobHunting版 - 请问这是什么level的面试题
vtable当然不变,不然怎么知道如何调用正确的虚函数。而且所有同类对象都用同一个
vtable。
这个是c++基本概念,应该搞清楚的。不是是否经常用cast的问题。
v****a
发帖数: 236
17
来自主题: JobHunting版 - 问几个关于G M A F面试的general问题
1. 不知道
2. 必然。。。distributed system, thread pool, producer/consumer, synchronize
, 各种读写锁的设计,更不要提web的各种题,至少被问过4次what happened after
typing a url。。语言也问的很多啊,abstract class和interface的区别, garbage
collection实现的机理,vtable的用处,多态是什么,印象最深的是面G onsite第三个
印度大叔问了vtable之后问java generic 的实现原理。。。面A onsite的时候也是一
个印度大叔问java里regular expression类的用法。。
3. 语言随便
plus: Lisp 是functional language...要是简历上写了lisp, scala, clojure之类的
估计很危险。。。没写不会问吧。
结论: 充分准备~~
s******n
发帖数: 3946
18
every object will have a pointer to the vtable for each virtual class.
So if an object inherit two virtual classes, it will have two pointers to
the two virutal classes, if this class added new virtual functions, then
this class itself will have a virtual table, so it will totally have three
pointers to vtable.
p*i
发帖数: 411
19
来自主题: JobHunting版 - 最新微软SDE II面试题
第一题的virtual table应该是啥样的?
我想的是
class D
vtable (A)
+0 D:f(); // (D*)->f() evaluates to this
vtable (B)
+0 A:f(); // (D*)->B::f() evaluates to this
(D*)->A::f() is ambiguous
第2题要么给A定义一个不带参数的ctor,要么在B的ctor中explicitly call A's ctor
还有最后static_cast(B)应该改成 static_cast(B*)
static_cast(B)(new A(1))会去找B的接受一个A*参数的ctor(不存在),而且返回的不是指针
h*****f
发帖数: 248
20
来自主题: JobHunting版 - 微软C++面试题
You will get a warning for the "incorrect" order of initialization for line
#14 as well.
Running 1, 2, 3 will have no warning or error.
#4 and 5 will have a runtime error (not compilation error as cplusplus.com
mentions).
#2 will have a compilation error (for all compilers?) if virtual D::foo()
isn't defined since the compiler doesn't know whether D::foo() should be
inherited from B or C.
#6 will have a runtime error because p3's vtable pointer is pointing to B's
vtable, and B has no virtual fun... 阅读全帖
G***l
发帖数: 355
21
来自主题: JobHunting版 - c++ vs Java virtual 实现(Y家)
因为在C++里,constructor执行完之前,object还不算存在。x = new B()会先调用A的
contructor,这时候x还不知道自己是B,那个vtable也没有建立,所以根本就无从调用B
的方法,就算你那个方法声明是virtual的,也没法dynamic binding,只能调用A里的。
java在没有执行A constructor的时候,类似c++ vtable的那些东西都已经建立好了。
所以能调用到B的方法。
但是不管在c++还是java里,constructor里调用virtual method都是很糟糕的东西,绝
对不应该做,面试的人问这个很脑残。
j*****n
发帖数: 1781
22
来自主题: Database版 - T-SQL Row Concatenate with a Twist??
Use PIVOT in SQL Server 2005 or upper versions...
;WITH VTable (ContactID, [Type], [Value])
AS
(
SELECT 123, 'email', 'a*[email protected]'
UNION ALL
SELECT 123,'phone', '555-555-5555'
UNION ALL
SELECT 123,'fax', '888-888-8888'
UNION ALL
SELECT 456, 'email', 'c*[email protected]'
UNION ALL
SELECT 456,'phone', '444-555-5555'
UNION ALL
SELECT 456,'fax', '444-888-8888'
)
SELECT ContactID, [email] AS Email, [phone] AS Phone, [fax] AS Fax
FROM ( SELECT ContactID, [Type], [Value]
FROM VTable ) P
PIVOT
( MAX([
c********e
发帖数: 383
23
来自主题: Programming版 - [转载] {rodney} Re: Hi
【 以下文字转载自 crystalike 的信箱 】
【 原文由 crystalike 所发表 】
basically think about how the compiler will generate code to
call the member function.
if the ptr type is of the class which has the non virtual function the compi
ler will generate the code which directly jump into the code segment of the
member function. static binding.
if the ptr tye is of the class which has the virtual function the compiler w
ill generate code to:
1. from the this pointer go the vtable.
2. from the vtable to call the appropr
c****e
发帖数: 1453
24
来自主题: Programming版 - How to check the virtual function table size?
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;
d*******d
发帖数: 2050
25
来自主题: Programming版 - one question about operator delete
我猜测啊,在你有virtual dtor的情况时,你delete 的时候,要先走dtor,然后是clas
s里面的delete,可是你delete了一个null,编译器无法从null找到vtable然后fetch出dt
or,所以直接就完了,没有走下去。
而你注释掉virtual dtor后,走的是default dtor,default dtor不用走vtable,所以总
是call了,default dtor没做什么,但是又接着call delete。
d****n
发帖数: 130
26
effective C++专门有一节讲这个问题。

can
b******k
发帖数: 1
E*****7
发帖数: 128
d****p
发帖数: 685
29
The second one overrides the first one in Derived. So in Derived's vtable
there is only one entry for clone()
whose offset is same as the clone() in Base's vtable.
Before C++98 overriding virtual member functions required both function
signature and return type match.
Now it is same as non virtual function overriding.
z****e
发帖数: 2024
30
来自主题: Programming版 - C++ Q20: construction and inheritance
是么?
我查了inside cpp model,
里边说,先构造B的基类,就是A和invoker,
然后才构造B自己的Vtable。
那么构造invoker的时候,B的Vtable还没有,所以就只能A的版本了。
不过如果你说是undefined,我学习一下。
X****r
发帖数: 3557
31
来自主题: Programming版 - 为什么derived object没有vptr?
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
32
来自主题: Programming版 - 为什么derived object没有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.
P********e
发帖数: 2610
33
来自主题: Programming版 - 为什么derived object没有vptr?
谢谢
那这个问题还是不太清楚:
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.
a****o
发帖数: 686
34
另外,virtual是会有不同的。
1. A::print是vritual,B::print如果声明,则需要定义。因为B::print此时会继承
virtual属性,故而,vtable需要一个函数地址。没有定义 B::print,则没有函数地址
给vtable去set up。
2. A::print不是vritual, B::print如果声明,不一定需要定义,只要B::print 不被
调用,编译可以通过,因为无需给改函数分配地址。
y*d
发帖数: 2226
35
来自主题: Programming版 - inline到底能省多少时间?

ODR是说你不能乱来,不是说compiler不能
举个例子
template
T add(const T& a, const T& b)
{
return a+b;
}
这个函数你放在.h里,有十个cpp里用过add()
那就有十个.o或者.obj都含有add
但是link的时候,会把多余的干掉
80年代的template实现要把所有的function都inline,还不吃static variable
但是90年代以后编译器就已经灰墙强悍了
ODR是为了帮你检查错误,怕你写了两个不同的define
对编译器自己,没这么多规矩
再举个例子,有虚函数的class都有个vtable
override和rtti都靠这个
请问这个vtable是在哪个.o里?
答案是每个.o里都有一个。因为编译器编译这个文件的时候,不知道别的.o里是否已经
有了,所以只能
是先放进去一个再说
然后呢?
link的时候,编译器会删掉多余的,只留一个
a****n
发帖数: 1887
36
来自主题: JobHunting版 - 问个C++重新编译的问题
都需要。。
a. add a constructor
如果class只有default constructor, 加入新的constructor, client code
A a; 就不work了
b. add a data member
class layout 改变, 肯定要重新编译,
c. change destructor into virtual
如果原class 没有vtable, 这个会改变class layout
d. add an argument with default value to an existing member function
函数签名改变, 调用到该函数的程序,需要重新编译
d**a
发帖数: 84
37
来自主题: JobHunting版 - 面试题
2.每个class都有个vtable,shared by all objects of that class. each object
has a pointer to this table.
5. look at log files, out put files. and inspect its entry under /proc/
for related info, e.g. smap. I did not give details and the interviewed
mentioned smap.
got onsite invitation later.
x***y
发帖数: 633
38
来自主题: JobHunting版 - a virtual table question by JP morgan
3, each of B, C, D part of a D object has vtptr and a corresponding vtable,
which stores the offset and some other values...
I**********s
发帖数: 441
39
来自主题: JobHunting版 - Google点面
问了1) 研究, 2) 多线程程序设计, 3) 任意无穷字符串流, 内存有限, 找出唯一一对
重复字符串, 这个我说了哈希表和外部排序, 但是面试人说有更好的办法(后来想也许
是bloom filter), 然后追问外部排序的细节到结束. 估计要挂 :(
总结: 面试既是技术活, 又是运气活.
无论如何, 把我的准备工作放下面, 攒点rp, 希望对大家有所帮助.
Interview Qs
Data Structures
1. Integer
- find number of 1s
- next largest smaller
- smallest larger number
- determine if is palindrom
- itoa, atoi
- add 2 numbers w/o using + or arithmetic operators
- implement *, -, / using only +
- find max of two numbers w/o co... 阅读全帖
f**********w
发帖数: 93
40
来自主题: JobHunting版 - one C++ question?
如果没有其他的virtual method,没有必要声明destructor virtual。因为这样会增加
额外的object空间开销,多了一个指向vtable的指针。
l*******o
发帖数: 791
41
来自主题: JobHunting版 - C++ Q21: size of virtual table
B, 因为每个含有若干virtual function的class只有一个vptr。这个“含有”包括即使
本类没有virtual function,但父类有,也就是继承下来的。所以A有一个VTABLE和一
个VPTR,B也有自己的一套。因为VPTR是在调用constructor时被insert进入的,所以
sizeof(B)就只是一个void * 的大小。在我的mac上是8
t*****j
发帖数: 1105
42
来自主题: JobHunting版 - C++ Q65: recompiling (IB)
选c是因为会改变其他子类的vtable,所以要重编译,对吗?
m*****n
发帖数: 2152
43
来自主题: JobHunting版 - C++ Q65: recompiling (IB)
如果这个class原来没有virtual function, 现在加了一个,object size也是变化的因为多了
vtable,这样和b的情况一样了。
t*****j
发帖数: 1105
44
来自主题: JobHunting版 - C++ Q65: recompiling (IB)
我觉得我思路可能错了,vtable应该是实例中才有的,也就是运行时候才有,和编译没
有关系。
这么排除下来应该还是b。

的因为多
h*****3
发帖数: 1391
45
来自主题: JobHunting版 - 小公司拒人的标准是什么?
我被问的只会比你多,不会比你少,到最后就只好说都不会了...
我还被问了vtable,coredump, tcp handshake,http post/get,ipv4/v6,linux调试,c语
言的static,.h文件,stack/heap,binary search, stl,LAMP,VLAN....
不知道这60分钟咋能问这么多.
p*****a
发帖数: 147
46
来自主题: JobHunting版 - virtual destructor (C++)问题
关于virtual destructor的实现:
也是用vtable实现的吗?
base class 和derived class 的virtual destructor 是不会override each other的
吧,所以在delete base class pointer to derived class object时,两个
destructor都会被call,
这样说对不对?
j*p
发帖数: 115
47
来自主题: JobHunting版 - 请问这是什么level的面试题
电面一个trading system developer的职位 印度人电面 本人不是计算机专业的
先问了一个brainteaser (一个unfair硬币做出fair的选择,这个过程结束时投硬币次
数的期望) 和c++的基本知识 比如stucture和class的区别 还有vtable怎么实现的
然后问一个derived类的指针cast到一个base类的指针是怎么实现的 从base类的指针到
derived类的指针 我从头到尾就没明白问的是啥 他大概说 那个指针是直接用还是要
copy一份再用或者什么的
我当时不记得cast的东西了 就跟他说 我只记得有四种cast类型 他说这是dynamic_
cast 怎么实现 然后就在这里纠缠了很久 我最后说 我对这个不了解 他问 你是面啥职
位啊 我说developer啊 然后 他就问其他题了 当时郁闷死了
之后就是两代算法题 一道是给一组数和一个给定值 找出两个数之和是那个给定值的所
有组合
另一道是找到一个数组第二大的数,comparison的次数是3n/2, in place.
电面之后我就去翻书 c++ primer上都没有仔细介绍ca... 阅读全帖
j*p
发帖数: 115
48
来自主题: JobHunting版 - 请问这是什么level的面试题
电面一个trading system developer的职位 印度人电面 本人不是计算机专业的
先问了一个brainteaser (一个unfair硬币做出fair的选择,这个过程结束时投硬币次
数的期望) 和c++的基本知识 比如stucture和class的区别 还有vtable怎么实现的
然后问一个derived类的指针cast到一个base类的指针是怎么实现的 从base类的指针到
derived类的指针 我从头到尾就没明白问的是啥 他大概说 那个指针是直接用还是要
copy一份再用或者什么的
我当时不记得cast的东西了 就跟他说 我只记得有四种cast类型 他说这是dynamic_
cast 怎么实现 然后就在这里纠缠了很久 我最后说 我对这个不了解 他问 你是面啥职
位啊 我说developer啊 然后 他就问其他题了 当时郁闷死了
之后就是两代算法题 一道是给一组数和一个给定值 找出两个数之和是那个给定值的所
有组合
另一道是找到一个数组第二大的数,comparison的次数是3n/2, in place.
电面之后我就去翻书 c++ primer上都没有仔细介绍ca... 阅读全帖
H***e
发帖数: 476
49
啥意思
有virtual function的话,不是得virtual destructor才可以有效clean
肯定得在vtable里面
d********t
发帖数: 9628
50
VTable是一个class一个,VPtr是每个object一个对吧?
1 2 下页 末页 (共2页)