Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!elroy.jpl.nasa.gov!sdd.hp.com!wuarchive!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c++ Subject: Re: Virtual function problem. Keywords: C++ virtual function Message-ID: <718@taumet.com> Date: 8 May 91 16:09:25 GMT References: <3472@trlluna.trl.oz> Organization: Taumetric Corporation, San Diego Lines: 41 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); >}; >"t.c", line 7: warning: B::fn() hides virtual A::fn() >"t.c", line 7: warning: B::fn() hides virtual A::fn() >Of course B::fn() hides A::fn(). That is why I wrote it! After all A::fn() is >virtual. >If I change the order of lines 1 and 2 (in class A), the problem goes away. This is a compiler bug, but not in the way you think. You should always get the warning. B::fn(int) hides (not overrides) both A::fn(char) and A::fn(char*) in that neither function is implicitly accessible from a B object. 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). In what you show, B::fn(int) is not virtual. So with your code we can get the following: B b; b.fn(2); // calls B::fn(int) b.fn('c'); // calls B::fn((int)'c'), not A::fn('c') b.fn("hello"); // illegal But you could still do this: b.A::fn('c'); // calls A::fn(char) b.A::fn("hello"); // calls A::fn(char*) 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*) -- Steve Clamage, TauMetric Corp, steve@taumet.com