我使用定义的接口库:成员函数指针
template<class desttype>
void connect(desttype* pclass, void (desttype::*pmemfun)());
,我有一个小的层次
class base {
void foo();
};
class derived: public base { ... };
在derived
一个成员函数,我想致电
connect(this, &derived::foo);
但似乎&derived::foo
实际上是一个成员函数指针的base
; GCC吐出
error: no matching function for call to ‘connect(derived* const&, void (base::* const&)())’
我可以明确地铸造this
到base *
解决这个问题;但为什么编译器不能匹配desttype = base
的调用(因为derived *
可以隐式转换为base *
)?
另外,为什么是&derived::foo
不是成员函数指针derived
?
是否有真的很好理由使用指向成员函数的指针,而不是仅仅在基础中声明(纯?)虚函数,并在需要时调用它? – 2010-04-08 18:01:40
@Jerry,你是什么意思? – 2010-04-08 18:13:06
@杰里棺材:当有 - 有。调用纯虚函数的唯一方法是通过该函数的硬编码名称。当硬编码特定函数不可接受时,使用指向成员的指针。这一定是这种情况之一。 – AnT 2010-04-08 18:15:29