Problem(?) with CINT and inheritance in function overloading

From: George Heintzelman (gah@bnl.gov)
Date: Tue Aug 31 1999 - 23:24:00 MEST


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