So I'm trying to access the virtual functions values using vtable. The way I understand it is that the compiler creates a pointer to the vtable, and a vtable is created for each class that contains a virtual function. I was able to get the values for the first two members. Now I don't understand how go about getting the values for 'bar' function.
#include <cstdio>
#include <iostream>
class Thing
{
private:
int x;
int y;
virtual int foo()
{
return x+y;
}
virtual int bar()
{
return x*y;
}
public:
Thing(){
x = 2;
y = 10;
}
};
int extract_x(void *thing)
{
// --- Begin your code ---
char* ptrA = (char*)thing;
ptrA=ptrA+8;
return *ptrA;
return 0;
// --- End your code ---
}
int call_bar(void* thing)
{
// --- Begin your code ---
// --- End your code ---
return 0;
}
int main()
{
Thing thing;
std::printf("%d %d\n",
extract_x(&thing),
call_bar(&thing));
return 0;
}