Ulrich Hobelmann wrote:
> Krivenok Dmitry wrote:
> > Hello all!
> >
> > Perhaps the most important feature of dynamic polymorphism is
> > ability to handle heterogeneous collections of objects.
> > ("C++ Templates: The Complete Guide" by David Vandevoorde
> > and Nicolai M. Josuttis. Chapter 14.)
> >
> > How to implement analogue of this technique via static polymorphism?
> > Perhaps there is special design pattern for this purpose...
>
>
> There are many concept that are called polymorphism or genericity:
> http://en.wikipedia.org/wiki/Polymor...ter_science%29
>
> Which ones do you mean (especially with "static polymorphism") exactly?
He means C++ templates, but the question is too broad to be answered
completely here. One could implement static polymorphism like this:
struct A
{
int Read(); // Does something
};
struct B
{
int Read(); // Does something else
};
template <class T>
class Reader
{
T& t_;
public:
Reader( T& t ) : t_( t ) {}
int DoSomething()
{
return t.Read();
}
};
This template is basically the same as using an abstract base class
with a virtual Read(), but it achieves the same effect without
virtuality, which can sometimes be useful.
Cheers! --M