Skip to content

Instantly share code, notes, and snippets.

@Frago9876543210
Created June 30, 2020 13:21
Show Gist options
  • Save Frago9876543210/5599a86c5cda490639d9073e72a44d73 to your computer and use it in GitHub Desktop.
Save Frago9876543210/5599a86c5cda490639d9073e72a44d73 to your computer and use it in GitHub Desktop.
get RTTI type name in runtime
#include <cstdio>
//black magic
struct Hacked {
void **vt;
};
template<typename T>
const char *getRuntimeTypename(T *type) {
auto type_info = (void **) ((Hacked *) type)->vt[-1];
auto type_name = (char *) type_info[1];
return type_name;
}
template<typename T>
void printType(T *type) {
printf("%s\n", getRuntimeTypename(type));
}
//
class X {
virtual void foo() {}
};
int main() {
X x;
printType(&x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment