std::type_info::operators

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

Checks if the objects refer to the same types.

Parameters

rhs - another type information object to compare to

Return value

true if the comparison operation holds true, false otherwise.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#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);
}

Output:

1
Paul is an employee who works in Economics

See also

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)
doc_CPP
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.