fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <typeinfo>
  4.  
  5. struct Shape
  6. {
  7. virtual ~Shape(){} // at least one virtual function needed for polymorphism
  8. };
  9.  
  10. struct Circle: Shape
  11. {
  12.  
  13. };
  14. struct Square: Shape
  15. {
  16.  
  17. };
  18.  
  19. int main()
  20. {
  21. std::vector<Shape *> vec;
  22. vec.push_back(new Square());
  23. vec.push_back(new Circle());
  24. std::vector<Shape *>::iterator tmp = vec.begin();
  25.  
  26. for(;tmp != vec.end(); ++tmp)
  27. {
  28. if(typeid(**tmp).name() == typeid(Square).name())
  29. {
  30. std::cout << "Square" << std::endl;;
  31. }
  32. else if(typeid(**tmp).name() == typeid(Circle).name())
  33. {
  34. std::cout << "Circle" << std::endl;;
  35. }
  36. }
  37.  
  38.  
  39. }
  40.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Square
Circle