fork download
  1.  
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <list>
  5.  
  6. using namespace std;
  7.  
  8. // Item
  9. class MyA { };
  10. class MyB { };
  11.  
  12. enum MyTypeEnum
  13. {
  14. myAType,
  15. myBType
  16. };
  17.  
  18. class MyItem
  19. {
  20. public:
  21. explicit MyItem(MyTypeEnum theType) : myType(theType) { };
  22. void Print();
  23.  
  24. private:
  25. MyTypeEnum myType;
  26. };
  27.  
  28. void MyItem::Print()
  29. {
  30. if(myType == myAType)
  31. cout << "MyItem - myA" << endl;
  32. else if(myType == myBType)
  33. cout << "MyItem - myB" << endl;
  34. }
  35.  
  36. // ItemList
  37. class MyItemList
  38. {
  39. public:
  40. void AddItem(MyItem theItem);
  41. void PrintItems();
  42.  
  43. private:
  44. list<MyItem> myList;
  45. };
  46.  
  47. void MyItemList::AddItem(MyItem theItem)
  48. {
  49. myList.push_back(theItem);
  50. }
  51.  
  52. void MyItemList::PrintItems()
  53. {
  54. for_each(myList.cbegin(), myList.cend(), [](MyItem i)
  55. {
  56. i.Print();
  57. });
  58. }
  59.  
  60. // main
  61. int main(int argc, char * argv[])
  62. {
  63. MyItemList itemList;
  64.  
  65. // itemList soll die Elemente tatsächlich besitzen
  66. {
  67. itemList.AddItem(MyItem(myAType));
  68. itemList.AddItem(MyItem(myBType));
  69. }
  70.  
  71. itemList.PrintItems();
  72.  
  73. return 0;
  74. }
  75.  
Success #stdin #stdout 0s 3016KB
stdin
Standard input is empty
stdout
MyItem - myA
MyItem - myB