fork download
  1. #include <iostream>
  2. #include <typeinfo>
  3. using namespace std;
  4.  
  5. struct A { virtual ~A() {}; virtual void foobar() {} };
  6. struct B : A { };
  7. struct C : A { };
  8. struct D : A { };
  9.  
  10. bool contains_type( A** arr, int ct, const type_info& a ) {
  11. for ( int idx = 0; idx < ct; ++idx ) {
  12. if ( a == typeid(*arr[idx]) )
  13. return true;
  14. }
  15.  
  16. return false;
  17. }
  18.  
  19. int main() {
  20. B b;
  21. C c;
  22. D d;
  23.  
  24. const int ct = 2;
  25. A* arr[ct] = {&b, &c};
  26.  
  27. cout << contains_type(arr,ct,typeid(B)) << endl;
  28. cout << contains_type(arr,ct,typeid(C)) << endl;
  29. cout << contains_type(arr,ct,typeid(D)) << endl;
  30. return 0;
  31. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
1
1
0