Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!usc!snorkelwacker.mit.edu!bloom-beacon!eru!hagbard!sunic!fuug!news.funet.fi!tukki.jyu.fi!sakkinen From: sakkinen@tukki.jyu.fi (Markku Sakkinen) Newsgroups: comp.lang.c++ Subject: Re: Overloaded Function Ambiguity Resolution Problems Message-ID: <1991Mar8.105837.15493@tukki.jyu.fi> Date: 8 Mar 91 10:58:37 GMT References: <1991Feb27.084909.5063@newcastle.ac.uk> <1991Feb27.204841.14690@world.std.com> Reply-To: sakkinen@jytko.jyu.fi (Markku Sakkinen) Organization: University of Jyvaskyla, Finland Lines: 54 In article <1991Feb27.204841.14690@world.std.com> wmm@world.std.com (William M Miller) writes: >Graham.Parrington@newcastle.ac.uk (Graham D. Parrington) writes: >> class Base >> { >> public: >> Base(char *) { cout << "Base::Base(char *)\n"; } >> void func(char*) { cout << "Base::func(char *)\n"; } >> }; >> >> class Derived:public Base >> { >> public: >> void func(const Base&){ cout << "Derived::func(const Base&)\n"; } >> >> }; >> >> main() >> { >> Derived *d = new Derived(); >> >> d->func("hello"); >> } >> >> What should it output? > >The cfront output is correct, G++ is wrong. See E&S section 13.1, page 310: >"Two function declarations of the same name refer to the same function if >they are in the same scope and have identical argument types (section 13). >A function member of a derived class is NOT in the same scope as a function >member of the same name in a base class." In this example, d->func("hello") >can only refer to Derived::func(const Base&), since Base::func(char*) is >hidden in the scope of Derived. > >-- William M. Miller, Glockenspiel, Ltd. > wmm@world.std.com The functions in the example have _not_ got identical argument types! The problem seems to be that a new function declared in a derived class hides _all_ functions with the same name in the base class, no matter what the number and types of arguments. If the constructor 'Base (char*)' were not there, the invocation of 'func' would evidently cause a compiler diagnostic. Current C++ has a lot of complicated and stupid visibility rules, as if expressly designed to shoot programmers in the back; this is one of them. Markku Sakkinen Department of Computer Science and Information Systems University of Jyvaskyla (a's with umlauts) PL 35 SF-40351 Jyvaskyla (umlauts again) Finland SAKKINEN@FINJYU.bitnet (alternative network address)