#include<iostream>
template<typename T>
class testClass {
public:
T a;
};
template<typename T>
void testFunc( void *a ) {
testClass<T> *tempClass = reinterpret_cast<testClass<T> *>(a);
tempClass->a++;
}
int main() {
void (*foo)(void *);
foo = testFunc<int>;
testClass<int> a;
a.a = 100;
std::cerr << "Before = " << a.a << "\n";
foo(&a);
std::cerr << "After = " << a.a << "\n";
return 0;
}
Can this template function testFunc be passed as function pointer to C functions safely ?? I am not clear how C++ is able to assign the correct memory address to the function pointer. The confusion is because I could not compile the same code in which I pass a class member function as function pointer argument.