1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
Having covered the construction of function templates, we're now ready for the
next step: constructing class templates. Many useful class templates already
exist. Rather than illustrating the construction of a class template by
looking at an already existing class template the construction of another
potentially useful new class template will be undertaken.
The new class implements a emi(circular queue). A circular queue has a fixed
number of tt(max_size) elements. New elements are inserted at its back and
only its head and tail elements can be accessed. Only the head element can be
removed from a circular queue. Once tt(n) elements have been appended the next
element is inserted again at the queue's (physical) first position. The
circular queue allows insertions until it holds tt(max_size) elements. As long
as a circular queue contains at least one element elements may be removed from
it. Trying to remove an element from an empty circular queue or to add another
element to a full circular queue results in exceptions being thrown. In
addition to other constructors a circular queue must offer a constructor
initializing its objects for tt(max_size) elements. This constructor must make
available the memory for the tt(max_size) elements but must not call those
elements default constructors (hinting at the use of the placement tt(new)
operator). A circular queue should offer value semantics as well as a move
constructor.
Please note that in the above description the actual data type that is used
for the circular queue is nowhere mentioned. This is a clear indication that
our class could very well be defined as a
hi(template: class)emi(class template). Alternatively, the class could be
defined for some concrete data type which is then abstracted when converting
the class to a class template.
The actual construction of a class template is provided in the next section,
where the class template tt(CirQue) (circular queue) is developed.
|