vptr is initialized by code generated by the compiler as part of the initialization obj. There is no magic, he just assigns it like this:
struct __A_vtbl_def {
void (*getdata)(__A*);
};
__A_vtbl_def __A_vtbl = {
&A__getdata
};
struct __A {
__A_vtbl_def* vptr;
int a;
};
__A obj;
obj.vptr = &__A_vtbl;
Note. This is all fake code showing how the compiler can implement vptr. I have no idea which particular code compilers are spitting out these days.
source
share