Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!mcnc!decwrl!bacchus.pa.dec.com!hollie.rdg.dec.com!decvax.dec.com!zinn!nuucp From: mjv@objects.mv.com (Michael J. Vilot) Newsgroups: comp.lang.c++ Subject: Re: Invoking Member Functions Via Pointers Message-ID: <1001@zinn.MV.COM> Date: 10 Nov 90 04:11:03 GMT Sender: nuucp@zinn.MV.COM Lines: 49 Anne Wilson asks for help: > I am having a devil of a time trying to invoke a pointer to member function > through an object which is a base class object for the derived class to > which the member function belongs. class Base_class { ... }; class Derived_class : public Base_class { void new_function(); }; typedef void (*v_fn_ptr) (void *); !! This is declaring a type named `v_fn_ptr' which points to a _non_ member !! function. class Container_class { ... void apply (v_fn_ptr); !! This declares a function taking a non-member function pointer }; c.apply (&Derived_class::new_function); // compiles, but crashes !! This should fail at compile time: it is placing a pointer of type !! `void (Derived_class::*)(void)' into a pointer of type `void (*)(void*)' !! It would make more sense to declare the type of `v_fn_ptr' as: typedef void (Base_class::* v_fn_ptr)(); > I have been trying the trick of letting the function appear like any old function > and passing it the address of the Derived_class object as its first argument, > but to no avail. I typed in the following example from Straustrup, and (guess > what ...) > > typedef void (*PROC) (void*, int); > It's a non-portable approach, at best. The example you cite, from ``The C++ Programming Language'' Section 5.4.5, has a footnote in the version of the book I have (second printing, March 1986): ``Later versions of C++ support a concept of pointer to member; cl::* means "pointer to member of cl". For example, typedef void (cl::*PROC)(int)'' Note the difference in type between this declaration, and the one above. Hope this helps, -- Mike Vilot, ObjectWare Inc, Nashua NH mjv@objects.mv.com (UUCP: ...!decvax!zinn!objects!mjv)