Re: templates and virtual
* Thomas Richter:
yes, templates and virtual member functions do not fit, I know. However,
I have here a situation where exactly that would be practical:
I have N worker classes that all provide the same interface, say:
class Interface {
public:
virtual double measure(const int *data1,const int *data2) = 0;
};
class A1 : public Interface {
// implements measure...
};
class A2 : public Interface {
// implements measure...
};
A clear application of virtual functions. Now, however, the same
function ("measure") is also required for a limited set of scalar types
that is known in advance. Say, "int", "short", "long" (doesn't matter
here). The implementation of "measure" is in all implementing interfaces
similar enough to share the code and generate it by a template.
Unfortunately, while it is know for which types I will need an
implementation for, the specific type required is not known at the type
an instance of the class is created, i.e. I cannot just template the
class itself by the type. It is known at call-time, but not at creation
time.
That is, I would prefer to write:
class Interface {
public:
template <typename T>
virtual double measure(const T *data1,const T* data2) = 0;
};
with classes implementing the interface requiring to implement the
template for all types T from a set of types specified in advance.
Clearly, the above is not possible in C++ (how would I tell the compiler
which T's are part of the interface specifications.
Which alternatives exist to generate a class hierarchy similar to the
above, i.e. require implementing classes of an interface to build
templates for a given set of types?
Here's one:
<code>
template< typename T >
class MeasureInterface
{
public:
virtual double measure( T const* data1, T const* data2 ) = 0;
};
#define DISAMBIGUATE( T ) \
virtual double measure( T const* data1, T const* data2 ) \
{ \
return MeasureImpl<T>::measure( data1, data2 ); \
}
template< template< class T> class MeasureImpl >
class MeasureFuncs
: public MeasureImpl<int>
, public MeasureImpl<double>
{
public:
DISAMBIGUATE( int )
DISAMBIGUATE( double )
};
template< typename T >
class A1MeasureFuncs
: public MeasureInterface< T >
{
public:
double measure( T const *, T const * )
{
return 1;
}
};
class A1
: public MeasureFuncs< A1MeasureFuncs >
{};
template< typename T >
class A2MeasureFuncs
: public MeasureInterface< T >
{
public:
double measure( T const *, T const * )
{
return 2;
}
};
class A2
: public MeasureFuncs< A2MeasureFuncs >
{};
#include <iostream>
int main()
{
using namespace std;
int i;
double d;
A1 a1;
cout << a1.measure( &i, &i ) << endl;
cout << a1.measure( &d, &d ) << endl;
MeasureInterface<int>& a1i = a1;
cout << a1i.measure( &i, &i ) << endl;
A2 a2;
cout << a2.measure( &i, &i ) << endl;
cout << a2.measure( &d, &d ) << endl;
MeasureInterface<int>& a2i = a2;
cout << a2i.measure( &i, &i ) << endl;
}
</code>
Cheers & hth.,
- Alf
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]