一、Typeinfo概述
Typeinfo是C++中的一个关键字,用于获取类型信息。它可以返回指向std::type_info对象的指针,这个对象包含了类型的名称和一些其他信息。
通过Typeinfo,我们可以在程序运行时确定一个对象的类型,并按照其类型进行相应的处理。 Typeinfo最开始是在C++的RTTI(Run-Time Type Identification)标准中引入的,可以作为动态多态的一种实现方式。
使用Typeinfo需要包含头文件。
二、获取Typeinfo
我们可以通过对象以及类型名来获取Typeinfo。
1. 通过对象获取Typeinfo
#include <typeinfo>
#include <iostream>
using namespace std;
int main() {
int n = 10;
const type_info &info = typeid(n);
cout << info.name() << endl;
return 0;
}
在上面的例子中,我们定义了一个int类型的变量n,然后使用typeid(n)获取Typeinfo,最后通过.name()方法输出了变量类型的名称。在此例中输出结果为“int”。
2. 通过类型名称获取Typeinfo
#include <typeinfo>
#include <iostream>
using namespace std;
int main() {
const type_info &info = typeid(int);
cout << info.name() << endl;
return 0;
}
在这个例子中,我们直接用typeid(int)来获取int类型的Typeinfo,输出结果也是“int”,与上一个例子相同。
三、Typeinfo用法
1. 在继承中使用Typeinfo
#include <typeinfo>
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() {}
};
class Circle : public Shape {
public:
void draw() {
cout << "draw a circle." << endl;
}
};
class Rectangle : public Shape {
public:
void draw() {
cout << "draw a rectangle." << endl;
}
};
int main() {
Circle c;
Shape *s = &c;
if (typeid(*s) == typeid(Circle)) {
s->draw();
}
return 0;
}
在这个例子中,我们先定义了一个基类Shape和两个派生类Circle和Rectangle,并且重写了继承的虚函数draw()。然后我们定义了一个Shape类型的指针s并将其赋值为Circle类型的对象,最后使用typeid(*s)来获取指针指向对象的Typeinfo,并判断是否等于Circle类型的Typeinfo。如果相等,就可以通过Circle类型的指针来调用draw()函数。
2. 在模板中使用Typeinfo
#include <typeinfo>
#include <iostream>
using namespace std;
template<typename T>
class Animal {
public:
void whoAmI() {
cout << typeid(T).name() << endl;
}
};
int main() {
Animal<int> a1;
Animal<char> a2;
a1.whoAmI();
a2.whoAmI();
return 0;
}
在这个例子中,我们使用模板来定义Animal类,并使用typeid(T)来获取模板类型的Typeinfo。在主函数中,我们分别定义了Animal<int>和Animal<char>类型的对象a1和a2,并分别调用了它们的whoAmI()函数,用于输出它们的类型名称。
3. 异常类型判断
#include <typeinfo>
#include <iostream>
#include <exception>
using namespace std;
int main() {
try {
throw std::runtime_error("Exception!");
} catch (const exception &e) {
if (typeid(e) == typeid(runtime_error)) {
cerr << e.what() << endl;
} else {
cerr << "Unknown exception." << endl;
}
}
return 0;
}
在这个例子中,我们使用了C++标准库中的std::exception和std::runtime_error异常类,并在try-catch语句中捕获了一些具有std::exception类型的异常。然后使用typeid(e)来获取捕获到异常的Typeinfo,并判断其是否等于std::runtime_error类型的Typeinfo。如果相等,输出异常信息;否则输出“Unknown exception.”。
四、实现自己的Typeinfo
我们可以重载运算符“operator==”来实现自定义类型的Typeinfo获取。
#include <typeinfo>
#include <iostream>
using namespace std;
class MyType {
public:
bool operator==(const type_info &info) const {
return (typeid(MyType) == info);
}
};
int main() {
MyType t;
const type_info &info = typeid(t);
if (info == typeid(MyType)) {
cout << "type matched." << endl;
}
return 0;
}
在这个例子中,我们定义了一个MyType类,并在其中重载了运算符“==”。在运算符中使用typeid(MyType)来获取MyType类型的Typeinfo,并在调用处使用相等运算符“==”来比较获取到的Typeinfo和自定义类型的Typeinfo。
五、小结
本文对Typeinfo进行了详细的阐述和分析。通过多个方面对Typeinfo的用法进行了介绍和示例,包括了通过对象和类型名称获取Typeinfo、在继承中使用Typeinfo、在模板中使用Typeinfo、异常类型判断和实现自己的Typeinfo。Typeinfo是动态多态的关键,并且在许多程序中有重要的作用。在使用Typeinfo时,还需要注意一些细节和限制,比如不能获取对象的完整信息等。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/250763.html
微信扫一扫
支付宝扫一扫