Created
May 11, 2020 13:06
-
-
Save yasukei/80ebc4a60eb226d7f4d0cad602e0b5bc to your computer and use it in GitHub Desktop.
std::typeinfo
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 <iostream> | |
#include <typeinfo> | |
namespace abc | |
{ | |
class Super1 | |
{ | |
public: | |
virtual void hoge() {} | |
}; | |
} // namespace abc | |
using namespace abc; | |
class Sub1 : public Super1 | |
{ | |
}; | |
class Sub2 : public Super1 | |
{ | |
}; | |
class SubSub1 : public Sub1 | |
{ | |
}; | |
int main() | |
{ | |
Super1* super1 = new Super1(); | |
Super1* sub1 = new Sub1(); | |
Super1* sub2 = new Sub2(); | |
Super1* subsub1 = new SubSub1(); | |
auto print = [=](Super1* ptr) { | |
std::cout << "typeid(*ptr).name(): [" << typeid(*ptr).name() << "]\n"; | |
std::cout << "typeid(*ptr).hash_code(): [" << typeid(*ptr).hash_code() << "]\n"; | |
}; | |
std::cout << "\n[Super1]\n"; | |
print(super1); | |
std::cout << "\n[Sub1]\n"; | |
print(sub1); | |
std::cout << "\n[Sub2]\n"; | |
print(sub2); | |
std::cout << "\n[SubSub1]\n"; | |
print(subsub1); | |
return 0; | |
} |
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
[Super1] | |
typeid(*ptr).name(): [class abc::Super1] | |
typeid(*ptr).hash_code(): [14028535803985416315] | |
[Sub1] | |
typeid(*ptr).name(): [class Sub1] | |
typeid(*ptr).hash_code(): [8470947193601520471] | |
[Sub2] | |
typeid(*ptr).name(): [class Sub2] | |
typeid(*ptr).hash_code(): [10343302246740559830] | |
[SubSub1] | |
typeid(*ptr).name(): [class SubSub1] | |
typeid(*ptr).hash_code(): [10692505005166377318] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment