Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!bagate!dsinc!unix.cis.pitt.edu!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!ira.uka.de!smurf!nadia!ananke!kaiser From: kaiser@ananke.stgt.sub.org (Andreas Kaiser) Newsgroups: comp.lang.c++ Subject: Invoking Member Functions Via Pointers Message-ID: <5.273AE276@ananke.stgt.sub.org> Date: 9 Nov 90 15:58:52 GMT Organization: Ananke, Stuttgart, FRG Lines: 51 In your msg to All, dated <09 Nov 90 08:33>, it said: AW> I am having a devil of a time trying to invoke a pointer to member AW> function through an object which is a base class object for the AW> derived class to which the member function belongs. The whole thing AW> is so compliciated, I have tried to "paraphrase" an example: You cannot invoke non-static member functions via normal function pointers. Member functions have a hidden first argument ("this"). For this reason, the C++ language has a special function pointer type, the "pointer to member function". Example: class Base_class { }; typedef void (Base_class::*v_fn_ptr)(void); // This is a pointer to a member function of // class "Base_class" or it's descendants. class Derived_class : public class { void new_function(); }; class Container_class { Base_class *list; Container_class (int obj_count, ... ); void apply (v_fn_ptr); }; Derived_class D1, D2; Container_class c (2, &D1, &D2); Now, if you want to run "apply": c.apply (Derived_class::new_function); and within "apply", this function is called by: (list->*function_ptr)(); Andreas -- :::::::::::::::::::: uucp: kaiser@ananke.stgt.sub.org :: Andreas Kaiser :: fido: 2:509/5.2512 & 2:507/18.7206 ::::::::::::::::::::