I want to define a class template that takes a callback function of the same type. Something like:
typedef template<class T> bool CallbackFn( T x );
template<class T> class MyClass
{
public:
MyClass() {}
~MyClass() {}
void addCallbackFn( CallbackFn* fn ) { callbackFn = fn; }
private:
CallbackFn* callbackFn;
};
And it would be used like this:
bool testFunctionInt(int x) { return true; }
bool testFunctionString(std::string x) { return true; }
MyClass<int> a;
a.addCallbackFn( testFunctionInt );
MyClass<std::string> b;
b.addCallbackFn( testFunctionString );
Unfortunately the callback function cannot be defined as a function template via the typedef
.
Is there another way to do this?