TITLE: Casting pointer to member functions PROBLEM: Randy Chapman I am trying to get some old (1991) code working, and it appears that current versions of C++ break it :( The basic juist of the problem is that I have a pointer type that is a pointer to a member function of class A. I want to assign to that variable (a member of class A, via a SetCallback()), a member function pointer of class B (same paramters, etc). Compiler (g++) gives me a casting error: > cat foo.C class A; typedef void (A::*Aptr)(); class A { Aptr foo; public: void SetFoo(char *event, Aptr fooP) { foo = fooP; } }; class B: protected A { void junk(); B() { SetFoo("SomeOddThing",junk); } }; > gcc foo.C foo.C: In method `B::B()': foo.C:14: type `B' is not a base type for type `A' foo.C:14: in pointer to member function conversion RESPONSE: clamage@Eng.Sun.COM (Steve Clamage), 7 Jun 95 First, you have a syntax error. The call to SetFoo should look like B() { SetFoo("SomeOddThing", &B::junk); } Second, casting among pointer-to-member-function works backwards from ordinary casts. Normally, you can cast a derived to a base, since a derived is a base (loosely speaking). But with pointer-to-member-function, the reverse is the case. A pointer to a Derived member function cannot be used where a pointer to Base member function is expected. The Base::*ptr will be used in conjunction with Base object, and a member of class Derived will expect to operate on a Derived object. In general, this will lead to disaster. (E.g., the Derived member function stores into data which is part of a Derived object, but isn't part of a Base object.) That's why it isn't allowed.