Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!bbn!apple!rutgers!att!ulysses!andante!alice!shopiro From: shopiro@alice.UUCP (Jonathan Shopiro) Newsgroups: comp.lang.c++ Subject: Re: address of virtual function (revisited) Summary: Pointers to virtual member functions are bound at time of call Message-ID: <9262@alice.UUCP> Date: 29 Apr 89 01:47:27 GMT References: <904@garya.Solbourne.COM> Organization: AT&T Bell Laboratories, Murray Hill NJ Lines: 66 In article <904@garya.Solbourne.COM>, garya@Solbourne.COM (Gary Aitken) writes: > Jonathan Shopiro's response to my earlier posting is a help in terms of > dereferencing the pointers once they have been given the proper values. > However, I don't see any solution (yet) to the problem of getting the > proper values in the pointers in the first place. > > How does one get the address of the virtual function for an object, given > a pointer to the object? > > class A { > public: > virtual void f() ; > } > class B : public A { > public: > void f() ; > } > > typedef void A::MEMF() ; // type for "member function of A" > MEMF *fp ; > B *bp = new B ; > A *objp = (A*) bp ; > > ... > > Or am I missing something here? If I do > fp = &A::f > and subsequently do a > (bp->*fp)() > will this somehow go through the virtual table and actually call B::f if > bp is REALLY a B* object pointer? It REALLY will. I promise. s o r r y a b o u t t h i s s i l l y s t u f f -- Jonathan Shopiro AT&T Bell Laboratories, Warren, NJ 07060-0908 research!shopiro (201) 580-4229