Hey all, I'm attempting to use an STL List to store a list of objects. Now I realise it's quite trivial to store a list of objects of my own definition.
My real question is, how do I fill the list with objects that inherit from a base class.
E.g. I have a class called Book which has the derived class Publication. How would I get the list to accept the base class aswell as derived classes?
currently I am doing it something like this:
list<Book> objectList; //define list Book temp("Title", "ISBN"); Publication pubTemp("Title", "ISBN", "Publisher"); objectList.push_back(temp); objectList.push_back(pubTemp);
This code compiles and runs fine, the issue however is that when attempting to create a publication object and store it in the list, it ignores the publisher argument. I realise that this is because the list isn't storing derived classes of Book. Afaik I'm supposed create a list of pointers to the base class of Book.
I have tried this:
list<Book*> bookList; Publication pubTemp("Title", "ISBN", "Publisher"); objectList.push_back(pubTemp);
However this produces the following error:
error C2664: 'std::list<_Ty>::push_back' : cannot convert parameter 1 from 'Book' to 'Book *const &'
I'm still fairly new with pointers, I understand the theory but the practice is another story. Anyway if anybody could help I'd appreciate it.