Question about member function pointers in a heirarchy
- by Jesse Beder
I'm using a library that defines an interface:
template<class desttype>
void connect(desttype* pclass, void (desttype::*pmemfun)());
and I have a small heirarchy
class base {
void foo();
};
class derived: public base { ... };
In a member function of derived, I want to call
connect(this, &derived::foo);
but it seems that &derived::foo is actually a member function pointer of base; gcc spits out
error: no matching function for call to ‘connect(derived* const&, void (base::* const&)())’
I can get around this by explicitly casting this to base *; but why can't the compiler match the call with desttype = base (since derived * can be implicitly cast to base *)?
Also, why is &derived::foo not a member function pointer of derived?