I want to have a templated class (wrapper), which can take all possible classes (T) and do stuff (here evaluate) with the member functions of these classes (function).
I found similar requests, which you can see here and here, but neither could satisfy the two conditions below.
Conditions:
Both, a pointer to the instance of the class ( T * ptr) and a pointer to the member function (function) must be accessible within the wrapper class.
The wrapper class shall work with both, const and non-const member functions.
Here a code that works only for non-const:
#include <iostream>
#include <math.h>
template< class T, double (T::*fck) (double) >
struct Wrapper
{
Wrapper( T * ptrT);
double evaluate( double );
protected:
T * myPtrT;
};
template< class T, double (T::*fck) (double) >
Wrapper<T, fck>::Wrapper( T * ptrT) : myPtrT(ptrT) {}
template< class T, double (T::*fck) (double) >
double Wrapper<T, fck>::evaluate( double s )
{ return (myPtrT->*fck)(s); }
struct kernel
{
double gauss( double s )
{
return exp(-0.5*s*s);
}
};
int main()
{
kernel G;
Wrapper<kernel, &kernel::gauss> myKernel ( &G );
std::cout<< myKernel.evaluate(0.0) <<std::endl;
std::cout<< myKernel.evaluate(0.3) <<std::endl;
return 0;
}