鍍金池/ 問答/C  C++  網(wǎng)絡(luò)安全/ 虛表在什么時候生成呢?存放在哪里呢?

虛表在什么時候生成呢?存放在哪里呢?

我們都知道C++中的多態(tài)是依賴于虛表和虛指針的,那么虛表是在什么時候生成的呢?是在構(gòu)造函數(shù)執(zhí)行之前還是在構(gòu)造函數(shù)之后之后呢?而且虛表存放在哪里呢?

回答
編輯回答
夏木

VMT is implementation dependent, so topics should be limited to compilers implementations rather than c++ itself.

那么虛表是在什么時候生成的呢. 是在構(gòu)造函數(shù)執(zhí)行之前還是在構(gòu)造函數(shù)之后之后呢?

For Most compilers, virtual table pointer initializes __vptr at constructor's initializer list.


而且虛表存放在哪里呢?

What confuses you is where __vptr is(not VMT, because VMT just consists of the addresses of trivial non-virtual functions which can be invoked by __vptr), then:

Compilers have multiple choices(all as a hidden member), you can refer to here

Summary: Where is the __vptr stored in an object (first or last are the usual answers).

Of course, you can implement VMT with c, then you will not ask such questions.

BTW, this is not good (even is a bad) question, because you even haven't searched it on google before asking, so -1.

Update:

Wikipedia is also your friend.

2017年2月19日 20:38