   std::list of base class?
  Hey, I'm wondering if it is possible to use a "std::list< MyClass >", but instead of storing MyClass objects in it, store objects from classes inherited from MyClass?I tried some things, but nothing never work, could someone give me an explanation on how would this be implemented?Thanksedit: Just to make things clear, what I really want to do is just be able to access different type of inherited class objects throught one std::list.. And I would use virtual fonctions[edited by - Hedos on May 12, 2004 8:30:17 PM][edited by - Hedos on May 12, 2004 8:32:28 PM][edited by - Hedos on May 12, 2004 8:32:39 PM]
  Then store (smart) pointers to the base class type. ;  But how would I create the objects? ;  With new would seem like the logical choice.std::list<Base *> my_list;my_list.push_back(new Derived1());my_list.push_back(new Derived2());  Just remember that you have to delete every pointer you new.;  Well, you don't *need* to use smart pointers to the base object... it will work fine if you just instantiate the subclass and pass it into the last (push_back or whatever)e.g.: #include <list>class MyClass {public:    virtual void doSomething(void);    // ... other functionality};class MySubClassA : public MyClass{public:    void doSomething(void); // one way to do it    // ... other functionality};class MySubClassB : public MyClass{public:    void doSomething(void); // another way to do it    // ... other functionality};using std::list;int main(void){    list<MyClass*> myList;    MyClass*   foo = new MySubClassA();    MyClass*   bar = new MySubClassB();    myList.push_back(foo);    myList.push_back(bar);    for (list<MyClass*>::iterator it = myList.begin();    it != myList.end();    it++)    {   (*it)->doSomething();   // this will polymorphically work    }} You may want to use smart pointers for safe memory allocation, though... since passing pointers will also allow you to down-cast easier ( using dynamic_cast(classPointer) ) EDIT: oops! wrote the code too quickly[edited by - void* on May 12, 2004 8:49:23 PM] [edited by - void* on May 12, 2004 8:51:08 PM];  That code (edit: the one in the first source block) suffers from major slicing issues. It won't work.edit: code I was referring to was editted out of existance   [edited by - SiCrane on May 12, 2004 9:05:47 PM];  could someone explain what a smart pointer is ? ;  You know, its not like you couldn''t throw "smart pointer" into google and get this as the first result.Basically a smart pointer is a class that work almost exactly like pointers but have some additional functionality. For example, reference counting or garbage collection. ;  thanks, i could have googled but id rather you explain, since you confused me  i knew about the boost::smart pointers, but you first said something about smart pointers, then gave an example with dumb pointers, which is why i got confused. thanks for the link ;  Thanks a lot, everything works now 