Dear George, As you found out, there is some difference in the way cint handles function overloading resolution. This case specifically, cint does not take level of inheritance into account. It simply calls what it finds first. ANSI/ISO C++ defines function overloading resolution as follows. 1. Exact match 2. Match with template instantiation 3. Match with type promotion 4. Match with standard conversion 5. Match with user defined conversion Cint does it in this way. Further, ANSI/ISO defines that in each matching stage, compiler has to find the best matching function among the candidate. Cint does not comply to this rule and simply calls what is found the first. Conversion to base class pointer is done in stage 4 (standard conversion). Within the standard conversion, cint takes whatever it finds the first. Cint is not aimed to be a 100%% ANSI/ISO compliant C/C++ language processor. It rather is a portable script language environment which is close enough to the standard C++. Thank you Masaharu Goto >Hi, > >I think CINT doesn't always find the right function to call. > >It is possible that what I am doing is a C++ error (or at least >undefined behavior), but I didn't think so... Test code follows: > >#include <iostream.h> > >class Base { >public: > int foo; >}; > >class Derived: public Base { >public: > int bar; >}; > >class Derived2: public Derived { >public: > int baz; >}; > >int func(Base *x) { > return x->foo; >} > >int func(Derived *x) { > return x->bar; >} > >/* >int main() { > Derived2 blah; > blah.baz = 1; > blah.bar = 2; > blah.foo = 3; > > Derived *blahp = &blah; > Base *blahp2 = &blah; > > cout << "Direct: " << func(&blah) << endl; > cout << "Derived *: " << func(blahp) << endl; > cout << "Base *: " << func(blahp2) << endl; >} >*/ > >If this code is loaded into root and the commented out main() executed >from the command line, I get: > >root [7] cout << "Direct: " << func(&blah) << endl; >Direct: 3 >root [9] cout << "Derived *: " << func(blahp) << endl; >Derived *: 2 >root [10] cout << "Derived *: " << func(blahp2) << endl; >Derived *: 3 > >whereas if I compile the code with egcs and run the a.out, I get: >Direct: 2 >Derived *: 2 >Base *: 3 > >I would guess that the problem is that in searching for something >matching >func(Derived2 *) it finds first in its list func(Base *), which >matches, and doesn't bother looking for a more derived match of the >function. > >From a strict reading of Stroustrup (3rd ed, pp. 149 - 150), it would >seem that the first call should actually be regarded as ambiguous, but >neither egcs nor root reported either an error or warning, and egcs's >behavior is much more intuitive. I couldn't find a clearer statement of >the official standard anywhere, unfortunately. > >George Heintzelman >georgeh@aya.yale.edu > >
This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:43:39 MET