运行时类型识别(run-time type identification, RTTI)的功能由两个运算符实现:
typeid
运算符,用于返回表达式的类型。dynamic_cast
运算符,用于将基类的指针或引用安全地转换成派生类的指针或引用。#include <iostream>
#include <typeinfo>
#include <string>
class Base
{
friend bool operator==(const Base&, const Base&);
public:
Base() = default;
Base(int i_) : i(i_) { }
protected:
virtual bool equal(const Base&) const;
private:
int i;
};
class Derived : public Base
{
public:
Derived() = default;
Derived(std::string s_, int i_) : s(s_), Base(i_) { }
protected:
bool equal(const Base&) const;
private:
std::string s;
};
bool operator==(const Base &lhs, const Base &rhs)
{
return typeid(lhs) == typeid(rhs) && lhs.equal(rhs);
}
bool Base::equal(const Base &rhs) const
{
return this->i == rhs.i;
}
bool Derived::equal(const Base &rhs) const
{
auto r = dynamic_cast<const Derived&>(rhs);
return (this->s == r.s) && this->Base::equal(rhs);
}
int main()
{
Base *pb1 = new Derived();
Base *pb2 = new Derived();
Base *pb3 = new Derived("a", 1);
Base *pb4 = new Base();
std::cout << std::boolalpha << (*pb1 == *pb2) << std::endl;
std::cout << std::boolalpha << (*pb1 == *pb3) << std::endl;
std::cout << std::boolalpha << (*pb1 == *pb4) << std::endl;
int arr[10];
Derived d;
std::cout << typeid(42).name() << ", "
<< typeid(arr).name() << ", "
<< typeid(d).name() << ", "
<< typeid(std::string).name() << ", "
<< typeid(pb1).name() << ", "
<< typeid(*pb1).name() << ", "
<< typeid(*pb3).name() << std::endl;
return 0;
}
typeid
表达式有两种形式:
type_info
或者type_info
的公有派生类型。const std::type_info &tiInt = typeid(int);
type_info
下列方法:
type_index类在头文件<typeindex>中声明,它是type_info对象的一个封装类,可以用作关联容器(比如map)和无序关联容器(比如unordered_map)的索引。
下面的代码(type_index)使用type_index来输出一些类型信息
#include <iostream>
#include <typeinfo>
#include <typeindex>
#include <unordered_map>
#include <string>
#include <memory>
struct A {
virtual ~A() {}
};
struct B : A {};
struct C : A {};
int main()
{
std::unordered_map<std::type_index, std::string> type_names;
type_names[std::type_index(typeid(int))] = "int";
type_names[std::type_index(typeid(double))] = "double";
type_names[std::type_index(typeid(A))] = "A";
type_names[std::type_index(typeid(B))] = "B";
type_names[std::type_index(typeid(C))] = "C";
int i;
double d;
A a;
// 注意我们正在存储指向类型 A 的指针
std::unique_ptr<A> b(new B);
std::unique_ptr<A> c(new C);
std::cout << "i is " << type_names[std::type_index(typeid(i))] << '\n';
std::cout << "d is " << type_names[std::type_index(typeid(d))] << '\n';
std::cout << "a is " << type_names[std::type_index(typeid(a))] << '\n';
std::cout << "b is " << type_names[std::type_index(typeid(*b))] << '\n';
std::cout << "c is " << type_names[std::type_index(typeid(*c))] << '\n';
}
输出
i is int
d is double
a is A
b is B
c is C
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有