www.digitalmars.com         C & C++   DMDScript  

D - Bug in polymorphic methods searching

reply "Mike Wynn" <mike.wynn l8night.co.uk> writes:
compiler beta v0.69

the following will not compile.
I have to add
 public void show( CA a, CB b ) { super.show( a, b ); }
to C;

and then also show( CA a ) { super.show( b ); }
to B to allow show( new CA() ); to be usable.

----------------------------------
import c.stdio;

class CA { }
class CB : CA { }

class A {
 public void show( CA a ) { printf("A::show( CA )\n"); }
}

class B : A {
 public void show( CA a, CB b ) { printf("B::show(CA, CB)\n"); }
}

class C : B {
 public void show( CA a ) { printf("C::show( CA )\n"); }
}

class D : C  {
}


int main( char[][] args ) {
 D b = new D();

 b.show( new CA(), new CB() );

 return 0;
}
// test_015.d(25): function show (CA a) does not match argument types (CA
,CB )
-----------
Aug 13 2003
parent reply "Walter" <walter digitalmars.com> writes:
Yes. C++ works the same way <g>.

"Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
news:bhed4u$1dre$1 digitaldaemon.com...
 compiler beta v0.69

 the following will not compile.
 I have to add
  public void show( CA a, CB b ) { super.show( a, b ); }
 to C;

 and then also show( CA a ) { super.show( b ); }
 to B to allow show( new CA() ); to be usable.

 ----------------------------------
 import c.stdio;

 class CA { }
 class CB : CA { }

 class A {
  public void show( CA a ) { printf("A::show( CA )\n"); }
 }

 class B : A {
  public void show( CA a, CB b ) { printf("B::show(CA, CB)\n"); }
 }

 class C : B {
  public void show( CA a ) { printf("C::show( CA )\n"); }
 }

 class D : C  {
 }


 int main( char[][] args ) {
  D b = new D();

  b.show( new CA(), new CB() );

  return 0;
 }
 // test_015.d(25): function show (CA a) does not match argument types (CA
 ,CB )
 -----------

Aug 13 2003
parent reply "Mike Wynn" <mike.wynn l8night.co.uk> writes:
"Walter" <walter digitalmars.com> wrote in message
news:bhenv4$1o2f$1 digitaldaemon.com...
 Yes. C++ works the same way <g>.

both of them "get it right" apart from "C++ does it that way" is there a good reason why D goes ?
 "Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
 news:bhed4u$1dre$1 digitaldaemon.com...
 compiler beta v0.69

 the following will not compile.
 I have to add
  public void show( CA a, CB b ) { super.show( a, b ); }
 to C;

 and then also show( CA a ) { super.show( b ); }
 to B to allow show( new CA() ); to be usable.

 ----------------------------------
 import c.stdio;

 class CA { }
 class CB : CA { }

 class A {
  public void show( CA a ) { printf("A::show( CA )\n"); }
 }

 class B : A {
  public void show( CA a, CB b ) { printf("B::show(CA, CB)\n"); }
 }

 class C : B {
  public void show( CA a ) { printf("C::show( CA )\n"); }
 }

 class D : C  {
 }


 int main( char[][] args ) {
  D b = new D();

  b.show( new CA(), new CB() );

  return 0;
 }
 // test_015.d(25): function show (CA a) does not match argument types


 ,CB )
 -----------


Aug 13 2003
parent reply "Mike Wynn" <mike.wynn l8night.co.uk> writes:
I though I better check the D docs ....

In D, function overloading is simple. It matches exactly, it matches with
implicit conversions, or it does not match. If there is more than one match,
it is an error.

this example has one exact match without even implicit conversion, it seems
to me that c++ is broken (what's the point of allowing overloading and
virtual function overridding if you can't actually use it!)

"Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
news:bhepm4$1pk4$1 digitaldaemon.com...
 "Walter" <walter digitalmars.com> wrote in message
 news:bhenv4$1o2f$1 digitaldaemon.com...
 Yes. C++ works the same way <g>.

both of them "get it right" apart from "C++ does it that way" is there a good reason why D goes ?
 "Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
 news:bhed4u$1dre$1 digitaldaemon.com...
 compiler beta v0.69

 the following will not compile.
 I have to add
  public void show( CA a, CB b ) { super.show( a, b ); }
 to C;

 and then also show( CA a ) { super.show( b ); }
 to B to allow show( new CA() ); to be usable.

 ----------------------------------
 import c.stdio;

 class CA { }
 class CB : CA { }

 class A {
  public void show( CA a ) { printf("A::show( CA )\n"); }
 }

 class B : A {
  public void show( CA a, CB b ) { printf("B::show(CA, CB)\n"); }
 }

 class C : B {
  public void show( CA a ) { printf("C::show( CA )\n"); }
 }

 class D : C  {
 }


 int main( char[][] args ) {
  D b = new D();

  b.show( new CA(), new CB() );

  return 0;
 }
 // test_015.d(25): function show (CA a) does not match argument types


 ,CB )
 -----------



Aug 13 2003
parent reply "Fabian Giesen" <rygNO SPAMgmx.net> writes:
 this example has one exact match without even implicit conversion, it
 seems to me that c++ is broken (what's the point of allowing
 overloading and virtual function overridding if you can't actually
 use it!)

In general, you can. But this rule (the so-called hiding rule) prevents some types of ambiguities to happen during name lookup/overload resolution. -fg
Aug 14 2003
parent reply "Mike Wynn" <mike.wynn l8night.co.uk> writes:
"Fabian Giesen" <rygNO SPAMgmx.net> wrote in message
news:bhh5jg$149q$1 digitaldaemon.com...
 this example has one exact match without even implicit conversion, it
 seems to me that c++ is broken (what's the point of allowing
 overloading and virtual function overridding if you can't actually
 use it!)

In general, you can. But this rule (the so-called hiding rule) prevents some types of ambiguities to happen during name lookup/overload

was this rule introduced with namespace ? (been doing mainly Java and C for the last few years, last time I programmed in C++ (apart for the odd bit of using MFC/WTL) was with Borland C++3.5 for DOS (and I'm sure that worked as expected [then again I'm sure that allowed ++i &= 7; !!]) found a solution with gcc (the `using` clause) think D should either follow the Java/C# rules or have a using clause to allow importing of the super classes method signatures. class CA { }; class CB : public CA { }; class A { public: virtual void show( CA * a ) { printf("A::show( CA )\n"); } }; class B : public A { public: virtual void show( CA * a, CB * b ) { printf("B::show(CA, CB)\n"); } }; class C : public B { public: using B::show; virtual void show( CA * a ) { printf("C::show( CA )\n"); } }; class D : public C { }; int main( int argc, char * argv[] ) { D * d = new D(); d->show( new CA(), new CB() ); return 0; }
Aug 14 2003
parent "Walter" <walter digitalmars.com> writes:
You're right. I'll look into making this work better.
Aug 15 2003