在线文档教程
C++
应用 | Utilities

std::type_info::operator!=

性病::类型[医]信息::Operator==,std::type[医]信息::操作员%21=

bool operator==( const type_info& rhs ) const;
bool operator!=( const type_info& rhs ) const;

检查对象是否引用相同的类型。

参数

rhs-another type information object to compare to

返回值

true如果比较操作成立,false否则。

二次

#include <iostream> #include <typeinfo> #include <string> #include <utility> class person { public: person(std::string&& n) : _name(n) {} virtual const std::string& name() const{ return _name; } private: std::string _name; }; class employee : public person { public: employee(std::string&& n, std::string&& p) : person(std::move(n)), _profession(std::move(p)) {} const std::string& profession() const { return _profession; } private: std::string _profession; }; void somefunc(const person& p) { if(typeid(employee) == typeid(p)) { std::cout << p.name() << " is an employee "; auto& emp = dynamic_cast<const employee&>(p std::cout << "who works in " << emp.profession() << '\n'; } } int main() { employee paul("Paul","Economics" somefunc(paul }

二次

产出:

二次

Paul is an employee who works in Economics

二次

另见

beforechecks whether the referred type precedes referred type of another type_index object in the implementation defined order, i.e. orders the referred types (public member function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/type/type[医]信息/操作员[医]CMP