Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!lll-winken!uunet!kddlab!ccut!titcca!sragwa!wsgw!socslgw!diamond!diamond From: diamond@diamond.csl.sony.junet (Norman Diamond) Newsgroups: comp.lang.c++ Subject: Re: Pointers to Methods Question Message-ID: <10208@socslgw.csl.sony.JUNET> Date: 2 May 89 09:40:00 GMT References: <797@usl.usl.edu> Sender: news@csl.sony.JUNET Reply-To: diamond@csl.sony.junet (Norman Diamond) Organization: /usr/lib/news/organization Lines: 59 In article <797@usl.usl.edu> pcb@usl.usl.edu (Peter C. Bahrs) writes: >am not sure why I receive the results below? Any comments? I >#include N > E >class What { W >public: S > What (void) { cout << form("Construct What\n"); } > void virtual print(); I >}; S > >void What::print() A >{ N > cout << form ("I am What's print routine\n"); >} A > S >class Where : public What { S >public: H > Where (void) { cout << form("Construct Where\n"); } O > void print(void) { cout << form ("Where's print routine\n"); } L >}; E > >main () >{ > typedef void What::WhatMember(); > typedef void Where::WhereMember(); > What* x = new What; > Where* y = new Where; > > WhatMember* fp1; > WhereMember* fp2; > fp1 = &Where::print; Interesting. I wonder why this isn't considered a type mismatch. > (x->*fp1)(); > fp2 = &Where::print; > (y->*fp2)(); >} >Construct What For your invocation of new What. >Construct What >Construct Where Both for your invocation of new Where. >I am What's print routine Since print() is virtual, (x->*fp1)() calls the version of print() which is defined for the actual type of (*x), which is What. >Where's print routine Similarly, the actual type of (*y) is Where. If print() were non-virtual, then Where::print() would be called both times. Norman Diamond, Sony Computer Science Lab (diamond%csl.sony.jp@relay.cs.net) The above opinions are my own. | Why are programmers criticized for If they're also your opinions, | re-inventing the wheel, when car you're infringing my copyright. | manufacturers are praised for it?