C++ Type_info 库 - operator== 函数
描述
它返回两个 type_info 对象标识的类型是否相同。
声明
以下是 std::type_info::operator== 的声明
C++98
bool operator== (const type_info& rhs) const;
C++11
bool operator== (const type_info& rhs) const noexcept;
参数
rhs − 它识别对象类型。
返回值
它返回两个 type_info 对象标识的类型是否相同。
异常
No-throw guarantee − 这个成员函数从不抛出异常。
数据竞争
语言环境对象被修改。
示例
在下面的 std::type_info::operator== 示例中。
#include <iostream>
#include <typeinfo>
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;
std::cout << "int vs my_int_type: ";
std::cout << ( typeid(int) == typeid(my_int_type) ) << '\n';
std::cout << "Base vs Derived: ";
std::cout << ( typeid(Base)==typeid(Derived) ) << '\n';
Base* pbase = new Derived;
std::cout << "Base vs *pbase: ";
std::cout << ( typeid(Base)==typeid(*pbase) ) << '\n';
Poly_Base* ppolybase = new Poly_Derived;
std::cout << "Poly_Base vs *ppolybase: ";
std::cout << ( typeid(Poly_Base)==typeid(*ppolybase) ) << '\n';
return 0;
}
输出应该是这样的 −
int vs my_int_type: true Base vs Derived: false Base vs *pbase: true Poly_Base vs *ppolybase: false