euaeny@euas10c19.ericsson.se (Erik Nyquist) (12/28/89)
According to "C++ - Reference Manual", page 26: "A pointer to a member of class B may be assigned to a pointer to a member of class D of the same type provided D is derived from B (directly or indirectly) by public derivation." I wanted to try this with the program below. The Sun C++-compiler gives the following error message: CC +g pointer-to-member.C: "pointer-to-member.C", line 30: error: bad assignment type: void (B::*)(char *) = void (D::*)(char *) */ Why isn't the program accepted? //------------------------------ #include <stream.h> #define NL <<"\n" class B { public: B(){} void b_method(char* cp) { cout << "Base: " << cp NL; } }; class D : public B { public: D(){} void d_method(char* cp) { cout << "Derived:" << cp NL; } }; main(){ D d; void (B::*bfunction)(char*) = B::b_method; (d.*bfunction)("Hallo, World!"); bfunction = D::d_method; (d.*bfunction)("Goodbye, World!"); } ----------------------------------------------------------- Erik Nyquist Tel: +46 - 8 719 9603 Ellemtel Utvecklings AB email: euaeny@euas18.ericsson.se Box 1505 S- 125 25 Stockholm Sweden ===========================================================
ldg@drywit.ATT.COM (XMRB30000-GibbonsD(DRR6702)262) (12/29/89)
From article <2580@erix.ericsson.se>, by euaeny@euas10c19.ericsson.se (Erik Nyquist): > > Why isn't the program accepted? > main(){ > D d; > void (B::*bfunction)(char*) = B::b_method; > (d.*bfunction)("Hallo, World!"); > bfunction = D::d_method; > (d.*bfunction)("Goodbye, World!"); > } > This code is an error and the Sun compiler correct. The line bfunction = D::method; is attempting to assign a ptr-to-D-member to to a ptr-to-B-member. The passage on pg 26 of the C++ Reference Manual sanctions just the opposite operation. -- -------------------------------------------------------------------------------- -- Doug Gibbons | ldg@druhi.ATT.COM or att!druhi!ldg -- AT&T Bell Laboratories -- Denver CO