#include #include #include "father.h" #include "derived.h" using namespace std; int main() { father fath(10); const father::layout& flay = reinterpret_cast(fath); cout << "father at address " << &fath << " has an f whose address is "<< reinterpret_cast(reinterpret_cast( flay.ptr_to_vtbl->ptr_to_f)) << ".\n" "Let's call this function twice, passing it " << &fath << ".\n"; fath.f(); flay.ptr_to_vtbl->ptr_to_f(&fath); //low-level way to do the same thing cout << "\n"; derived d(20, 30, 40); const derived::layout& dlay = reinterpret_cast(d); cout << "derived at address " << &d << " has an f whose address is " << reinterpret_cast(reinterpret_cast( dlay.ptr_to_vtbl->ptr_to_f)) << ".\n" "Let's call this function twice, passing it " << &d << ".\n"; d.f(); dlay.ptr_to_fvtbl->ptr_to_f(&d); //low-level way to do the same thing cout << "\n"; const father *const p = &d; const father::layout& flay2 = reinterpret_cast(*p); cout << "father at address " << p << " has an f whose address is " << reinterpret_cast(reinterpret_cast( flay2.ptr_to_vtbl->ptr_to_f)) << ".\n" "Let's call this function twice, passing it " << p << ".\n"; p->f(); flay2.ptr_to_vtbl->ptr_to_f(p); //low-level way to do the same thing return EXIT_SUCCESS; }