This is the Drill for Chapter 19 on page 707.
template<typename T> struct S { T val; };
.T
.types S<int>
, S<char>
, S<double>
, S<string>
, and
S<vector<int>> ;
initialize them with values of your choice.get()
that returns a reference to val
.get()
outside the class.get()
.set()
function template so that you can change val
.set()
with an S<T>::operator=(const T&)
. Hint: Much simpler
than §19.2.5.const
versions of get()
.template<typename T> read_val(T& v)
that reads from
cin
into v
.read_val()
to read into each of the variables from 3 except the
S<vector<int>>
variable.>>
and <<
) for vector<T> s
.
For both input and output use a { val, val, val }
format. That will allow
read_val()
to also handle the S<vector<int>>
variable.
Remember to test after each step.Exercise 2 of Chapter 19 on page 709.
Write a template function that takes a vector<T> vt
and a vector<U> vu
as
arguments and returns the sum of all vt[i]*vu[i]
s.
Exercise 3 of Chapter 19 on page 709.
Write a template class Pair
that can hold a pair of values of any type.
Use this to implement a simple symbol table like the one we used in the
calculator (§7.8).
Exercise 10 of Chapter 19 on page 709.
Implement a simple unique_ptr
supporting only a constructor, destructor,
–>
, *
, and release()
. In particular, don’t try to implement an assignment
or a copy constructor.
Exercise 11 of Chapter 19 on page 709.
Design and implement a counted_ptr<T>
that is a type that holds a
pointer to an object of type T
and a pointer to a “use count” (an int
)
shared by all counted pointers to the same object of type T
. The use
count should hold the number of counted pointers pointing to a given
T
. Let the counted_ptr
’s constructor allocate a T
object and a use count
on the free store. Let counted_ptr
’s constructor take an argument to be
used as the initial value of the T
elements. When the last counted_ptr
for a T
is destroyed, counted_ptr
’s destructor should delete the T
. Give
the counted_ptr operations that allow us to use it as a pointer. This is
an example of a “smart pointer” used to ensure that an object doesn’t
get destroyed until after its last user has stopped using it. Write a set
of test cases for counted_ptr
using it as an argument in calls, container
elements, etc.