Skip to content

Instantly share code, notes, and snippets.

@onqtam
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onqtam/a3f33e688314d7b52867 to your computer and use it in GitHub Desktop.
Save onqtam/a3f33e688314d7b52867 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <typeinfo>
struct A {
//virtual ~A(){} //#uncomment to enable rtti for this inheritance hierarchy
};
struct B : A {};
void func(A* a1, A* a2) {
if(typeid(*a1) == typeid(*a2))
printf("they are the same!\n");
//# do not compile unless there is polymorphism
//if(dynamic_cast<B*>(a1))
// printf("wtf1\n");
//if(dynamic_cast<B*>(a2))
// printf("wtf2\n");
printf("%s\n", typeid(*a1).name());
printf("%s\n", typeid(*a2).name());
}
int main(int argc, char** argv) {
A a;
B b;
func(&a,&b);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment