Created
June 30, 2020 13:21
-
-
Save Frago9876543210/5599a86c5cda490639d9073e72a44d73 to your computer and use it in GitHub Desktop.
get RTTI type name in runtime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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