I want to be able to call a member function (eg. doSomething()
in class testClass
) outside the class using some sort of functional template. In short, be able to call a non-static member function somewhat in this manner, functionCaller(t.doSomething)
from main()
.
Say I have a C++ class like:
class testClass {
public:
testClass()
{
value = 0;
}
int prod(int i)
{
value *= i;
return value;
}
int doSomething()
{
value = prod(10);
return value;
}
private:
int value;
};
My functional template looks something like this:
template<typename T, typename ret>
ret callFunction(T t, ret (T::*func)()) {
// checkSomePermissionsHere()
return t.func()
}
If I try to use this template in the following manner:
int main()
{
testClass t1;
callFunction(t1, &testClass::doSomething);
return 0;
}
I get this error:
error: no member named 'func' in 'testClass'
return t.func(a);
^
What is the correct way to call this member function (doSomething
) on object t1
through the callFunction
method? I want to implement callFunction
as part of a general API which does some checks before executing the provided fucntion.
I'm using clang/LLVM 3.5 for my compiler.