Xref: utzoo comp.lang.c++:10130 comp.std.c++:382 Path: utzoo!attcan!uunet!mcsun!hp4nl!fwi.uva.nl!delft From: delft@fwi.uva.nl (Andre van Delft) Newsgroups: comp.lang.c++,comp.std.c++ Subject: Re: Address of member function Message-ID: <1401@carol.fwi.uva.nl> Date: 29 Oct 90 00:01:51 GMT Sender: news@fwi.uva.nl Reply-To: delft@fwi.uva.nl (Andre van Delft) Followup-To: comp.lang.c++ Organization: Faculteit Wiskunde & Informatica, Universiteit van Amsterdam Lines: 61 steve@taumet.com (Steve Clamage) writes: > >delft@fwi.uva.nl (Andre van Delft) writes: > > >I meant the following: why is it allowed to take the address of: > > >a GLOBAL VARIABLE > >a GLOBAL FUNCTION > >a MEMBER VARIABLE, i.e. a variable local to an instance of a class > > >but why is it NOT allowed to take the address of > > >a [nonstatic] MEMBER FUNCTION, i.e. a function local to an instance of a class > >The problem comes with virtual functions. Suppose that you could take >the address of a virtual member function. You would still have to call >it in conjuction with some object (so "this" could refer to something). >Suppose we had something like > class C { ... public: virtual f(); g();... }; > class D : public C { ... public: virtual f(); ... }; > D d; > > void (*fp)() = c.f; // presently illegal > C* cp = &d; > >1. cp->f(); // virtual call, in this case calls dp->D::f() >2. cp->fp(); // invented sytax -- what do we do here? >3. fp(); // ok if fp had the address of an ordinary function I would *never* propose the "invented syntax" cp->fp() since fp is a name of a global variable instead of a name of a member in class C. I do not see why virtual functions would be a problem for my proposal: a new "access modifier", e.g., 'present', for member functions so that you can take their addresses (which you can also do with member variables), as in class C {present void f(void);} ... C anObject; void (*fp)(void); ... fp = anObject.f; ... fp(); The preprocessor/compiler would 1. place in *each* instance 'anObject' of a class C, and for each of its 'present' functions 'f(parms)' simple code that calls the code C::f(&anObject,parms) (which is shared among all objects of the class for space efficiency). 2. transform a call to a 'present' function f(parms) in anObject into something like ( (void(*)(parms) (&anObject.f) ) (parms) instead of something like C::f(anObject,parms) No problem with virtual functions: each object instance will know itself to what class it belongs, and implement the 'present' function accordingly. Andre van Delft DELFT@fwi.uva.nl