fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5.  
  6. struct Entity;
  7. static std::vector<Entity*> entities;
  8.  
  9. struct Entity
  10. {
  11. std::string entityName;
  12.  
  13. Entity(const std::string &name) : entityName(name)
  14. {
  15. entities.push_back(this);
  16. }
  17.  
  18. Entity(const Entity &src)
  19. : Entity(src.entityName)
  20. {
  21. }
  22.  
  23. ~Entity()
  24. {
  25. auto iter = std::find(entities.begin(), entities.end(), this);
  26. if (iter != entities.end())
  27. entities.erase(iter);
  28. }
  29.  
  30. void Draw()
  31. {
  32. // do drawing things
  33. std::cout << "drawing " << entityName << " ..." << std::endl;
  34. }
  35. };
  36.  
  37. Entity player("player");
  38. Entity enemy("enemy");
  39.  
  40. void renderEntities()
  41. {
  42. for (auto *entity : entities)
  43. {
  44. entity->Draw();
  45. }
  46. }
  47.  
  48. int main()
  49. {
  50. renderEntities();
  51. return 0;
  52. }
Success #stdin #stdout 0s 4472KB
stdin
Standard input is empty
stdout
drawing player ...
drawing enemy ...