Skip to content

Instantly share code, notes, and snippets.

@iwagaki
Last active August 29, 2015 14:15
Show Gist options
  • Save iwagaki/f70912541bd1c7b938ae to your computer and use it in GitHub Desktop.
Save iwagaki/f70912541bd1c7b938ae to your computer and use it in GitHub Desktop.
When does a sub-class override a super-class method?
#include <cstdio>
void checkVTBL0andCall(void* ptr)
{
long long* ptr_object = (long long*)ptr;
printf("ptr_object = %llx\n", (long long)ptr_object);
long long vptr = ptr_object[0];
printf("vptr = %llx\n", (long long)vptr);
long long* vtbl = (long long*)vptr;
printf("vtbl[0] = %llx\n", (long long)vtbl[0]);
void (*f)() = (void (*)())vtbl[0];
f();
return;
}
class ClassA
{
public:
ClassA()
{
checkVTBL0andCall(this);
method(); // will call ClassA::method()
}
virtual void method()
{
printf("ClassA::method()\n");
}
void method2()
{
method(); // will call ClassB::method()
}
};
class ClassB : public ClassA
{
public:
void method()
{
printf("ClassB::method()\n");
}
};
int main()
{
ClassA* ptr = new ClassB;
checkVTBL0andCall(ptr);
ptr->method2();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment