[ fromfile: inheritance-intro.xml id: managedcontainers ]
Qt's value containers are containers of uniform (same-typed) values - e.g. QString, byte
, int
, float
, etc.
Pointer containers are containers of pointers to (polymorphic commonly-typed) objects. They can be managed or unmanaged.
Both kinds of containers can grow at runtime by allocating additional heap memory as needed. This is always done in an exception-safe way, so you don't need to worry about possible memory leaks.
In the case of pointer containers to heap objects, however, one must decide which class is responsible for managing the heap objects. UML diagrams can distinguish between managed and unmanaged containers by using composite (filled diamond) and aggregate (empty diamond) connectors, as shown in Figure 6.6.
In general, a managed container is a composite, because the container manages its pointed-to objects. In other words, when a composite is destroyed, it destroys (cleans up) its entire self because the smaller objects are part of its composition.
When one object embeds another as a subobject, it is also considered a composition.
In Figure
6.6, there are two kinds of Customer
containers: CustomerList
and CustDb
.
CustDb
and CustomerList
both reuse template containers.
CustomerList
objects are aggregates – temporary structures to hold the results of a query, or a user selection.
CustDb
, on the other hand, is a singleton composite that manages all the Customer
objects that exist.
In the case of the Customer
and Address
relationship, this diagram indicates that one or more Address
objects should be associated with a particular Customer
.
When the Customer
object is destroyed, it is reasonable to destroy all its Address
objects at the same time.
Thus, the Customer
object manages its Address
es,
which gives you another example of a composite relationship.
Note | |
---|---|
This suggested design does impose some limitations on possible use of |
Typically, a managed container deletes any heap objects it "owns" when the container itself is destroyed.
With a Qt container of pointers, you can use qDeleteAll(container)
, an algorithm that
calls delete
on each element in the container.
Copying a managed container can be defined in a number of ways:
For some containers, the feature might be disabled.
For others, it might be defined as a deep copy, where all contained objects are cloned and placed in the new container.
Another approach, taken with the design of Qt containers, is implicit sharing, explained in Section 11.5.
An aggregate container is a container that provides only an indexing or reference navigation mechanism to its contents.
In this case, the container does not manage its objects – it only provides a convenient way to access them. When an aggregate container is copied, only references to the collected objects are copied. When an aggregate container is deleted, only the references are removed. There is no impact on the underlying objects in the container.
Note | |
---|---|
A managed container is a composition, and an unmanaged container of objects is usually (but not always) represented in a UML diagram as aggregation. |
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |