我有一个这样的模板类:
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;
}
getValue代码应该使用_this参数作为&#34; this&#34;来调用存储在this-&gt; classFuncVar点的方法。这个电话。我试着写这个:
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);
}
但它没有用,我收到了这个错误:
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’|
我尝试了几种变体,包括括号,但我没有使它成功。 该行的语法应该如何正确?
提前致谢!
答案 0 :(得分:1)
在getValue
方法代码中需要更多的括号,以便在调用成员函数之前将方法指针绑定到其目标对象:
return (_this->*classFuncVar)(x);
// or more complete
return (_this->*(this->classFuncVar))(x);