1

I'm doing a program that manages a collection of items. That can be a Book, a Magazine, a CD or a DVD. Each of those is a class thats inherits the class Item. To store those items I'm using the list template, like this:

list<Item> items;

and this list is inside an object lib of the class Library.

To run through this list I'm doing this:

for(list<Item>::iterator i = lib.itens.begin(); i != lib.itens.end(); ++i)

Until this point everything's fine. The problem starts when I try to call a method of the derived class inside this loop. example:

    for(list<Item>::iterator i = lib.itens.begin(); i != lib.itens.end(); ++i)
        (*i).lib.itens.show();

How can I call those methods?

Gonzo
  • 1,533
  • 16
  • 28

2 Answers2

2

There are at least two problems here. Firstly, if you do this:

list<Item> items;

then this list really will only contain Item objects; if you try to put in a derived object, the derived part will simply be sliced off.

One solution is to use a list of pointers instead (although you should probably use smart pointers to avoid memory-management issues).

But even then, the second issue is that you shouldn't (in general) be trying to call derived-class-specific methods via pointers to the base class. The whole point of polymorphism is that you should only be dealing in terms of base-class pointers if you're happy to work with functionality that's common to the whole hierarchy (see Liskov substitution principle).

Community
  • 1
  • 1
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • ok, and how can I do a list that's able to store objects of any derived classes? – Gonzo May 25 '12 at 01:47
  • You need to use list instead of list. References are polymorphic. – Carl May 25 '12 at 01:54
  • and how to access those items? and add items to the list? – Gonzo May 25 '12 at 02:05
  • 1
    @carleeto: References are polymorphic, but not objects. Since `list` can only contain objects, you can't create a `list`. That's why Oli suggested a list of pointers: pointers are both polymorphic and objects. – MSalters May 25 '12 at 07:30
0

You probably should have defined virtual void show() = 0; in class Item. That would have made the show call legal, but at the same time would have resulted in an error on list<Item>.

The fundamental error is that you can't have something that's "just" an Item, yet list<Item> would attempt to make a list of precisely that. By declaring show as a pure virtual function in Item, the compiler explicitly knows this.

MSalters
  • 173,980
  • 10
  • 155
  • 350