struct test
{
void f() {};
};
test t1;
using memfun_t = void (test::*)();
memfun_t mf = &test::f;
auto a1 = &test::f; // OK
auto a2 = t1.*mf; // error
auto a3 = &(t1.*mf); // still no luck有什么不可以推断的吗?我希望得到引用标准的答案。
编辑:
我找到了一个名为__closure的RAD语言扩展,它似乎解决了这个问题。
class base {
public:
void func(int x) {
};
};
typedef void(base::*pBaseMember)(int);
class derived : public base {
public:
void new_func(int i) {
};
};
int main(int argc, char * argv[]) {
derived derivedObject;
void(__closure * derivedClosure)(int);
// Get a pointer to the ‘new_func’ member.
// Note the closure is associated with the
// particular object, ‘derivedObject’.
derivedClosure = derivedObject.new_func;
derivedClosure(3); // Call ‘new_func’ through the closure.
return 0;
}http://docwiki.embarcadero.com/RADStudio/Seattle/en/Closure
发布于 2017-02-16 23:25:59
你不能用
auto a2 = t1.*mf; // error 就像你不能用:
auto a2 = t1.f;t1.f不是一个有效的表达式。不能通过类的实例获得指向成员函数的指针。与非成员函数不同,非成员函数在使用时会衰减为函数指针,成员函数不会衰减到成员函数指针。
C++11标准的相关文本:
一元算子 ..。 4只有在使用显式
&且其操作数是限定id而不是括在括号内时,才会形成指向成员的指针。注意:也就是说,表达式&(qualified-id)(其中的qualified-id括在括号中)并不构成“指向成员的指针”类型的表达式。qualified-id也是如此,因为不存在从非静态成员函数的限定id到“指针到成员函数”类型的隐式转换,就像从函数类型的值到类型“指针到函数”(4.3)一样。&unqualified-id也不是指向成员的指针,即使在非限定id类的范围内也是如此.-end注记
https://stackoverflow.com/questions/42286590
复制相似问题