fork download
  1. #include <iostream>
  2. #include <map>
  3. using namespace std;
  4.  
  5. namespace sf
  6. {
  7. class Sprite
  8. {
  9. public:
  10. void introduceYourself() const
  11. {
  12. cout<<"Sprite\n";
  13. }
  14. };
  15.  
  16. class Sound
  17. {
  18. public:
  19. void introduceYourself() const
  20. {
  21. cout<<"Sound\n";
  22. }
  23. };
  24. }
  25.  
  26. class CResource
  27. {
  28. public:
  29. virtual ~CResource() {}
  30. virtual void doSomething() const = 0;
  31. };
  32.  
  33. class CSound : public CResource
  34. {
  35. public:
  36. void doSomething() const
  37. {
  38. sound.introduceYourself();
  39. }
  40.  
  41. private:
  42. sf::Sound sound;
  43. };
  44.  
  45. class CSprite : public CResource
  46. {
  47. public:
  48. void doSomething() const
  49. {
  50. sprite.introduceYourself();
  51. }
  52.  
  53. private:
  54. sf::Sprite sprite;
  55. };
  56.  
  57. class CResourceManager
  58. {
  59. public:
  60. CResourceManager()
  61. {
  62. resourcesMap[0] = new CSprite;
  63. resourcesMap[1] = new CSprite;
  64. resourcesMap[2] = new CSound;
  65. resourcesMap[3] = new CSound;
  66. resourcesMap[4] = new CSprite;
  67. }
  68. ~CResourceManager()
  69. {
  70. for( const auto &res : resourcesMap )
  71. delete res.second;
  72. }
  73.  
  74. void printResources()
  75. {
  76. for( const auto &res : resourcesMap )
  77. res.second->doSomething();
  78. }
  79.  
  80. private:
  81. map<int, CResource*> resourcesMap;
  82.  
  83. };
  84.  
  85. int main() {
  86. CResourceManager resMngr;
  87.  
  88. resMngr.printResources();
  89. return 0;
  90. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
Sprite
Sprite
Sound
Sound
Sprite