C++运行时库的判定
先上代码:
#include <iostream>
#include <typeinfo>
using namespace std;
class Base {
};
class Derived1 :public Base {};
class Derived2 :public Base {};
int main(void) {
Base b;
const type_info &info1 = typeid(b);
if (info1 == typeid(Base))
cout << "b is base class!" << endl;
else
cout << "b is Derived class!" << endl;
Derived1 d1;
const type_info &info2 = typeid(d1);
if (info2 == typeid(Derived2))
cout << "d1 is Derived2 class!" << endl;
else
cout << "d1 is Derived1 class!" << endl;
system("pause");
return 0;
}
运行得到:
b is base class!
d1 is Derived1 class!
所以判定是正确的。