Class Templates
- Stroustrup says
- Templates add flexibility
- They have nearly optimal performance.
- One word of caution, the compiler will not generate code that is not needed.
- So if you don't test thoroughly, you may have a syntax error in your code you do not discover.
- This is particularly problematic if you have a class method that is not called.
- The standard container library is built on top of this.
- We will look this in more detail later.
- The basic concept remains the same
- To keep things familiar you can do the class declaration and class implementation in different steps (ie the old .h and .cpp file)
- You will need to each function in separate
templatestatements - And you will need the strange construct
-
template < typename T> FooT<T>::FooT () { }
- Look at ArrayT.h
- I wrapped everything in a
templatewrapper, - made the changes above, (ie ArrayT:: becaeme ArrayT<DataT>::)
- I wrapped everything in a
- In the test file, I needed to make a few small changes.
-
ArrayTbecameArrayT<DataT>
-
- I created an integer test as well in intDriver.cpp