Sorry, too tired right now to give much of an explanation. Perhaps this code will help you wrap your head around adding things to the list.
Note that you can add/remove items at the head/tail of the list. Have a look into std::list.pop_front and front over
here[
^]
#include <iostream>
#include <list>
#include <stdlib.h>
using namespace std;
class shape
{
public:
shape(){}
virtual ~shape(){cout<<"boom\n";}
virtual char *getType() = 0;
private:
};
class circle : public shape
{
public:
char *getType() {return "circle";}
};
class square : public shape
{
public:
char *getType() {return "square";}
};
class triangle : public shape
{
public:
char *getType() {return "triangle";}
};
typedef list<shape*> shapeList_t;
typedef shapeList_t::iterator shapeListIter_t;
int main()
{
int i, numItems=10;
shapeList_t myList;
shapeListIter_t myIter;
shape *curShape;
for (i=0; i<numItems; i++)
{
switch (rand() % 3)
{
case 0: myList.push_back(new circle); break;
case 1: myList.push_back(new square); break;
case 2: myList.push_back(new triangle); break;
}
}
for (myIter=myList.begin(); myIter!=myList.end(); myIter++)
{
curShape = *myIter;
cout << "Shape type: " << curShape->getType() << endl;
}
for (myIter=myList.begin(); myIter!=myList.end(); myIter++)
{
curShape = *myIter;
delete curShape; }
myList.clear();
return 0;
}
EDIT: Just realized I made a mess of things - the correct destructor for each shape isn't called - only the base-class's one is. Mea Culpa.
Fixed - forgot to add the virtual keyword to the base class destructor.