Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!sdd.hp.com!spool.mu.edu!uunet!brunix!sdm From: sdm@cs.brown.edu (Scott Meyers) Newsgroups: comp.lang.c++ Subject: Re: Virtual function problem. Keywords: C++ virtual function Message-ID: <74942@brunix.UUCP> Date: 8 May 91 18:47:03 GMT References: <3472@trlluna.trl.oz> <718@taumet.com> Sender: news@brunix.UUCP Reply-To: sdm@cs.brown.edu (Scott Meyers) Organization: Brown University Department of Computer Science Lines: 37 In article <718@taumet.com> steve@taumet.com (Stephen Clamage) writes: | mcf@tardis.trl.OZ.AU (Michael Flower) writes: | | >class A { | > virtual void fn(char); // 1 | > virtual void fn(char *); // 2 | >}; | >class B : public A { | > void fn(int); | >}; ... | A virtual function in a derived class overrides (not hides) one | in a base class if the signatures are the same (and the return types | must match as well). ... | If you declare B::f(int) explicitly virtual, you will get: | b.fn(2); // calls B::fn(int) | b.fn('c'); // calls A::fn(char) | b.fn("hello"); // calls A::fn(char*) Nope, even when B::fn is virtual, it still hides the two versions of A::fn: b.fn(2); // calls B::fn(int) b.fn('c'); // calls B::fn(int) b.fn("hello"); // illegal Remember that overloaded functions are disambiguated statically, and the scope of B hides the scope of A. Hence all references to the identifier fn through an object of (static) type B will resolve to the fn in B's scope. Scott ------------------------------------------------------------------------------- What do you say to a convicted felon in Providence? "Hello, Mr. Mayor."