Reputation: 117
I have a template class like this:
class template <class T1, class T2, class CTHIS> class cachedValuesClass
{
typedef T2 (CTHIS::*ptrToInternalMethodType)(T1) const;
ptrToInternalMethodType classFuncVar;
T2 getValue(CTHIS* _this, T1 x, bool *error = NULL) const;
}
The getValue code is supposed to call for the method stored at the point this->classFuncVar using the _this parameter as the "this" for this call. I tried writing this:
template <class T1, class T2, class CTHIS>
T2 cachedValuesClass<T1, T2, CTHIS>::getValue(CTHIS* _this, T1 x, bool *error /*=NULL*/) const
{
return *_this.*this.*classFuncVar(x);
}
But it doesn't work, I get this error:
130|error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘((const cachedValuesClass<float, float, vocationForTheorical>*)this)->cachedValuesClass<float, float, vocationForTheorical>::classFunc (...)’, e.g. ‘(... ->* ((const cachedValuesClass<float, float, vocationForTheorical>*)this)->cachedValuesClass<float, float, vocationForTheorical>::classFunc) (...)’|
130|error: ‘this’ cannot be used as a member pointer, since it is of type ‘const cachedValuesClass<float, float, vocationForTheorical>* const’|
I tried several variations, including parenthesis but I coudn't make it work. How should be correct syntax for this line?
Thanks in advance!
Upvotes: 1
Views: 87
Reputation: 13438
You need a few more parenthesis in your getValue
method code, in order to bind the method pointer to its target object before calling the member function:
return (_this->*classFuncVar)(x);
// or more complete
return (_this->*(this->classFuncVar))(x);
See also: How to call through a member function pointer?
Upvotes: 1