Section 15.7. Containers and Inheritance


15.7. Containers and Inheritance

We'd like to use containers (or built-in arrays) to hold objects that are related by inheritance. However, the fact that objects are not polymorphic (Section 15.3.1, p. 577) affects how we can use containers with types in an inheritance hierarchy.

As an example, our bookstore application would probably have the notion of a basket that represents the books a customer is buying. We'd like to be able to store the purchases in a multiset (Section 10.5, p. 375). To define the multiset, we must specify the type of the objects that the container will hold. When we put an object in a container, the element is copied (Section 9.3.3, p. 318).

If we define the multiset to hold objects of the base type

      multiset<Item_base> basket;      Item_base base;      Bulk_item bulk;      basket.insert(base);  // ok: add copy of base to basket      basket.insert(bulk);  // ok: but bulk sliced down to its base part 

then when we add objects that are of the derived type, only the base portion of the object is stored in the container. Remember, when we copy a derived object to a base object, the derived object is sliced down (Section 15.3.1, p. 577).

The elements in the container are Item_base objects. Regardless of whether the element was made as a copy of a Bulk_item object, when we calculate the net_price of an element the element would be priced without a discount. Once the object is put into the multiset, it is no longer a derived object.

Because derived objects are "sliced down" when assigned to a base object, containers and types related by inheritance do not mix well.



We cannot fix this problem by defining the container to hold derived objects. In this case, we couldn't put objects of Item_base into the containerthere is no standard conversion from base to derived type. We could explicitly cast a base-type object into a derived and add the resulting object to the container. However, if we did so, disaster would strike when we tried to use such an element. In this case, the element would be treated as if it were a derived object, but the members of the derived part would be uninitialized.

The only viable alternative would be to use the container to hold pointers to our objects. This strategy worksbut at the cost of pushing onto our users the problem of managing the objects and pointers. The user must ensure that the objects pointed to stay around for as long as the container. If the objects are dynamically allocated, then the user must ensure that they are properly freed when the container goes away. The next section presents a better and more common solution to this problem.

Exercises Section 15.7

Exercise 15.28:

Define a vector to hold objects of type Item_base and copy a number of objects of type Bulk_item into the vector. Iterate over the vector and generate the net_price for the elements in the container.

Exercise 15.29:

Repeat your program, but this time store pointers to objects of type Item_base. Compare the resulting sum.

Exercise 15.30:

Explain any discrepancy in the amount generated by the previous two programs. If there is no discrepancy, explain why there isn't one.




C++ Primer
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2006
Pages: 223
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net