std::type_info::operator==, std::type_info::operator!=
提供: cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev ">
</tbody><tbody>
</tbody>
bool operator==( const type_info& rhs ) const; |
(C++11未満) | |
bool operator==( const type_info& rhs ) const noexcept; |
(C++11以上) | |
bool operator!=( const type_info& rhs ) const; |
(C++11未満) | |
bool operator!=( const type_info& rhs ) const noexcept; |
(C++11以上) (C++20未満) |
|
オブジェクトが同じ型を参照しているか調べます。
引数
| rhs | - | 比較する別の型情報オブジェクト |
戻り値
比較結果が真であれば true、そうでなければ false。
例
Run this code
#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
関連項目
処理系定義の順序に従って、参照先の型が別の type_info の参照先の型より前かどうか、確認します。 つまり、参照先の型を順序付けます (パブリックメンバ関数) |