Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!rpi!ispd-newsserver!tuna!randolph From: randolph@tuna.ssd.kodak.com (Gary L. Randolph) Newsgroups: comp.lang.c++ Subject: Re: Overloaded Function Ambiguity Resolution Problems Message-ID: <1991Feb28.135548.29427@ssd.kodak.com> Date: 28 Feb 91 13:55:48 GMT References: <1991Feb27.084909.5063@newcastle.ac.uk> Sender: randolph@ssd.kodak.com Organization: Eastman Kodak Lines: 55 In article <1991Feb27.084909.5063@newcastle.ac.uk> Graham.Parrington@newcastle.ac.uk (Graham D. Parrington) writes: gdp>Consider the following code fragment: gdp>class Base gdp>{ gdp>public: gdp> Base() { cout << "Base::Base()\n"; } gdp> Base(char *) { cout << "Base::Base(char *)\n"; } gdp> void func(char*) { cout << "Base::func(char *)\n"; } gdp>}; gdp>class Derived:public Base gdp>{ gdp>public: gdp> Derived() { cout << "Derived::Derived()\n"; } gdp> void func(const Base&){ cout << "Derived::func(const Base&)\n"; } gdp>}; gdp>main() gdp>{ gdp> Derived *d = new Derived(); gdp> d->func("hello"); gdp>} gdp>What should it output? gdp>Cfront 2.1 (<>) gives: gdp> Base::Base() gdp> Derived::Derived() gdp> Base::Base(char *) gdp> Derived::func(const Base&) gdp>while g++ (version 1.39.0 beta (based on GCC 1.39)) gives: gdp> Base::Base() gdp> Derived::Derived() gdp> Base::func(char *) gdp>That is, g++ considers Base::func(char*) to be the correct match to the call gdp>while cfront prefers to construct a Base first and then use Derived::func(Base) gdp>My reading of the ARM pp318-> makes me believe that g++ is correct in this case gdp>since there is an exact match on argument type (rule 1), whereas cfront appears gdp>to apply rule 4 (user defined conversion). I suspect the differences lie in gdp>search strategies in the two compilers, that is, g++ applies the rules in order gdp>over the entire hierachy, while cfront applies the rules first on the current gdp>class and then on its parents etc. gdp>What do other compilers make of this example (Zortech, Glockenspiel, etc), and gdp>more importantly which is the CORRECT behaviour?? Cfront has the correct behavior. What you have here is one of the most common mistakes I have seen people make. DERIVATION IS *NOT* OVERLOADING. See page 310 of the ARM. The name of the function, func, exists in two DIFFERENT scopes. The derived class essentially hides Base::func. But I PUBLICly derive you say? True, that is why the following is legal: d->Base::func(char *); private derivation would make this statement illegal. I post this rather than emailing because it is a VERY common mistake. Gary Randolph