Dynamic casting of a container class
Hello all,
In a signal processing project I use C++ container classes similar to
std::vector but specialized for DSP requirements, so that there is an
abstract non-template class container from which other template
classes inherit, plus some polymorphic methods to allow easy
implementation of the factory design pattern.
class container {
public:
virtual container* clone()=0;
}
template<typename T>
class kernel1D : public container {
public:
virtual container* clone();
};
template<typename T>
class kernel2D : public container {
public:
virtual container* clone();
};
At runtime, the user might need to cast one kernel type to another one
(e.g. kernel1D<int> to kernel1D<float>) but he has just a
container*. Does anybody know a design pattern / generic programming
construct that would allow to do this kind of casting at runtime in an
elegant way? Right now there is just a huge chain of conditionals
(i.e. if (dynamic_cast<kernel1D<T> > (ptr) != 0) {} else if (...) ) to
do this, but I'm sure there has to be a much more elegant and
efficient solution to create this kind of code at compile time by
using templates, or some polymorphic clever inheritance pattern.
The cast is usually done within the same container type (i.e. from
kernel1D<T> to kernel1D<U> or from kernel2D<T> to kernel2D<U>, but
never between kernel1D to kernel2D.
Any hints will be greatly appreciated.
Regards,
Pablo
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]