Since I do not think the order of virtual functions in source could affect their order in machine code, I try to give a counter example. The main idea is first to give a layout for the vtable
of a base class
struct A
{
void virtual test() {};
void virtual replay() {};
}
then using two classes which inherit A
, but with different order of virtual functions:
struct B : public A
{
void virtual test() {};
void virtual replay() {};
}
struct C : public A
{
void virtual replay() {};
void virtual test() {};
}
If the order of virtual functions in B
and C
affects their machine code order, then their pointers in corresponding vtables
should be different. But the following generated machine code (I have used clang
as the compiler) shows that they are not:
.rodata:0804888C ; vtable for B
.rodata:0804888C _ZTV1B db 0 ; DATA XREF: B::B(void)+1Co
...
.rodata:08048894 dd offset _ZN1B4testEv ; B::test(void)
.rodata:08048898 dd offset _ZN1B6replayEv ; B::replay(void)
...
...
.rodata:08048904 ; vtable for C
.rodata:08048904 _ZTV1C db 0 ; DATA XREF: C::C(void)+1Co
...
.rodata:0804890C dd offset _ZN1C4testEv ; C::test(void)
.rodata:08048910 dd offset _ZN1C6replayEv ; C::replay(void)
The order of virtual functions in B
and C
are indeed the same (and respects one in A
).