?????麯????C++???????????????????Σ????????C++?麯??????C++?????????????????csdn?????????????????VC?麯???????????????? ???????????????麯???????????C++????????????????????????????????????????
?????????C++?麯???????????????????????????????????????????????????linux???μ?g++?????????????????????????麯???????????????????????????????????
??????????????????????????Щ??л????????????????????????????????C????????????????????
????1. ????
???????????????????????????麯?????????????????????????????????£?
classBase
{
public:
virtualvoidf(){}
virtualvoidg(){}
};
classDerive:publicBase
{
public:
virtualvoidf(){}
};
intmain()
{
Derived;
Base*pb;
pb=&d;
pb->f();
return0;
}
????2. ????????麯????(vtable)
???????g++ –Wall –S test.cpp???????????????C++????????????????????
????ZTV4Base:
????.long   0
????.long   _ZTI4Base
????.long   _ZN4Base1fEv
????.long   _ZN4Base1gEv
????.weak   _ZTS6Derive
????.section    .rodata._ZTS6Derive??"aG"??@progbits??_ZTS6Derive??comdat
????.type   _ZTS6Derive?? <a href="http://www.jobbole.com/members/anduo1989">@object</a>
????.size   _ZTS6Derive?? 8
????_ZTV4Base??????????????????????????????g++???????????????????????????????????C++??????????????c++filt??????????????磺
????[lyt@t468 ~]$ c++filt _ZTV4Base
????vtable for Base
????_ZTV4Base????(???????)????????????飬??????????0???????_ZIT4Base?????Base???????????????typeid?й????????????????????????????? ???Base???vtable???????????????C??????????£?
????unsigned long Base_vtable[] = {
????&Base::f()??
????&Base::g()??
????};
??????Derive?????????????????е????
????ZTV6Derive:
????.long   0
????.long   _ZTI6Derive
????.long   _ZN6Derive1fEv
????.long   _ZN4Base1gEv
????.weak   _ZTV4Base
????.section    .rodata._ZTV4Base??"aG"??@progbits??_ZTV4Base??comdat
????.align 8
????.type   _ZTV4Base?? <a href="http://www.jobbole.com/members/anduo1989">@object</a>
????.size   _ZTV4Base?? 16
?????????C??????????£?
????unsigned long Derive_vtable[] = {
????&Derive::f()??
????&Base::g()??
????};
?????????????????vtable?????????Derive??vtable?е???????д??Base??vtable?????????????д???????麯???????????vtable???????????????vtable??? ???????????????????????????????????vtable?????????????????檔
????3. ???????? vtable ?????
??????????????????????????vtable???????????????????????麯?????????????????????vtable?????????vtable????????????????? ???????????????????????????
????_ZN4BaseC1Ev:
????.LFB6:
????.cfi_startproc
????.cfi_personality 0x0??__gxx_personality_v0
????pushl   %ebp
????.cfi_def_cfa_offset 8
????movl    %esp?? %ebp
????.cfi_offset 5?? -8
????.cfi_def_cfa_register 5
????movl    8(%ebp)?? %eax
????movl    $_ZTV4Base+8?? (%eax)
????popl    %ebp
????ret
????.cfi_endproc
????Base::Base()???????????????£?
????ZN4BaseC1Ev?????????C++????Base::Base() ??????????????????c++flit?????????C++???class??????????????????????????????????????????????????????????????????????????????麯?????
???????????Base???У???????????????????????????vtable?????Base?????壬????д???????????C????
????struct Base {
????unsigned long **vtable;
????}
???????????й???????????
????movl    8(%ebp)?? %eax
????movl    $_ZTV4Base+8?? (%eax)
????$_ZTV4Base+8 ??Base????麯???????λ??????????????????C???????£?
????void Base::Base(struct Base *this)
????{
????this->vtable = &Base_vtable;
????}
??????????Derive???????????£?
????struct Derive {
????unsigned long **vtable;
????};
????void Derive::Derive(struct Derive *this)
????{
????this->vtable = &Derive_vtable;
????}