I need to set a function pointer variable which is a method of a template class X to a method of X.
Here is a simple example.
X.h:
template<typename T>
class X {
public:
typedef T (*valproc)(T v);
X(T v);
T add(T v);
T val;
valproc curproc;
};
X.cpp:
#include "X.h"
template<typename T>
X<T>::X(T v) : val(v) {
curproc = &X<T>::add;
}
template<typename T>
T X<T>::add(T v) {
return v+val;
}
int main (int iArgC, char *apArgV[]) {
X<int> *p = new X<int>(3);
return p->curproc(7);
}
When I compile this, I get an error:
$ g++ -c -g -Wall X.cpp
X.cpp: In instantiation of 'X<T>::X(T) [with T = int]':
X.cpp:15:29: required from here
X.cpp:5:13: error: cannot convert 'int (X<int>::*)(int)' to 'X<int>::valproc {aka int (*)(int)}' in assignment
curproc = &X<T>::add;
Apparently
int (X< int >::* )(int)is not the same as
int (*)(int)
How can I define the correct type?