首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::type_info::operator!=

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

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

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

参数

rhs

-

another type information object to compare to

返回值

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

二次

代码语言:javascript
复制
#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);
}

二次

产出:

二次

代码语言:javascript
复制
Paul is an employee who works in Economics

二次

另见

before

checks 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)

代码语言:txt
复制
 © cppreference.com

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

扫码关注腾讯云开发者

领取腾讯云代金券