makc.the.great@gmail.com wrote:[color=blue]
> now that I am looking at templates, there's the question.
>
> why same effect couldn't/shouldn't be achieved with inheritance?
>
> provided the difference of the two, when and where do I use templates
> instead of inheritance, and other way around?
>
> thoughts, please.[/color]
Templates allows a variety of interesting and useful constructs that
are not available (or are *really* inconvenient) with inheritance
alone. See the Boost libraries and _Modern C++ Design_ for some good
examples. For instance, you can create a policy-based design using
templates, such as this policy-based smart pointer from the Loki
library:
template
<
typename T,
template <class> class OwnershipPolicy,
class ConversionPolicy,
template <class> class CheckingPolicy,
template <class> class StoragePolicy[color=blue]
>[/color]
class SmartPtr
: public StoragePolicy<T>
, public OwnershipPolicy<typename StoragePolicy<T>::PointerType>
, public CheckingPolicy<typename StoragePolicy<T>::StoredType>
, public ConversionPolicy
{
// use policy members here
};
(See also the improvements to the original design in the CUJ artiles
starting with
http://www.cuj.com/documents/s=8890/...r/alexandr.htm .)
Cheers! --M