1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
// comparing type_info objects
#include <iostream> // std::cout
#include <typeinfo> // operator typeid
struct Base {};
struct Derived : Base {};
struct Poly_Base {virtual void Member(){}};
struct Poly_Derived: Poly_Base {};
typedef int my_int_type;
int main() {
std::cout << std::boolalpha;
// fundamental types:
std::cout << "int vs my_int_type: ";
std::cout << ( typeid(int) == typeid(my_int_type) ) << '\n';
// class types:
std::cout << "Base vs Derived: ";
std::cout << ( typeid(Base)==typeid(Derived) ) << '\n';
// non-polymorphic object:
Base* pbase = new Derived;
std::cout << "Base vs *pbase: ";
std::cout << ( typeid(Base)==typeid(*pbase) ) << '\n';
// polymorphic object:
Poly_Base* ppolybase = new Poly_Derived;
std::cout << "Poly_Base vs *ppolybase: ";
std::cout << ( typeid(Poly_Base)==typeid(*ppolybase) ) << '\n';
return 0;
}
|