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> #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("sairamkrishna","tutorialspoint"); somefunc(paul); }
输出应该是这样的 −
sairamkrishna is an employee who works in tutorialspoint