Question about member function pointers in a heirarchy

Posted by Jesse Beder on Stack Overflow See other posts from Stack Overflow or by Jesse Beder
Published on 2010-04-08T17:51:36Z Indexed on 2010/04/08 17:53 UTC
Read the original article Hit count: 345

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?

© Stack Overflow or respective owner

Related posts about c++

Related posts about member-function-pointers