Thread overview
polymorphism into derived classes
Aug 24, 2003
Helmut Leitner
Aug 25, 2003
Mike Wynn
August 24, 2003
This code:

class A { void a(float b) { printf("%f\n",b); } }
class B:A { void a(int b) { printf("%d\n",b); } }
void main() {
    B b=new B;
    b.a(3);
    b.a(4.5);
}

Outputs
3
4

To print correctly 4.5 I need to do b.A.a(4.5), which I personally find unatractive. So why, just like C++, doesn't D support polymorphism into derived classes?

Hey, but at least C++ lets us bring the methods from the base class, while D doesn't.

-------------------------
Carlos Santander
This code:

class A { void a(float b) { printf("%f\n",b); } }
class B:A { void a(int b) { printf("%d\n",b); } }
void main() {
    B b=new B;
    b.a(3);
    b.a(4.5);
}

Outputs
3
4

To print correctly 4.5 I need to do b.A.a(4.5), which I personally find unatractive. So why, just like C++, doesn't D support polymorphism into derived classes?

Hey, but at least C++ lets us bring the methods from the base class, while D doesn't.

-------------------------
Carlos Santander


August 24, 2003

"Carlos Santander B." wrote:
> 
> This code:
> 
> class A { void a(float b) { printf("%f\n",b); } }
> class B:A { void a(int b) { printf("%d\n",b); } }
> void main() {
>     B b=new B;
>     b.a(3);
>     b.a(4.5);
> }
> 
> Outputs
> 3
> 4
> 
> To print correctly 4.5 I need to do b.A.a(4.5), which I personally find unatractive. So why, just like C++, doesn't D support polymorphism into derived classes?

I think allowing this would at least also require a way to block methods from parent classes.

On the other hand I think you could write

 class B:A {
   void a(int b) { printf("%d\n",b); }
   void a(float b) { super.a(b); }
 }

easily.

--
Helmut Leitner    leitner@hls.via.at Graz, Austria   www.hls-software.com
August 25, 2003
"Helmut Leitner" <helmut.leitner@chello.at> wrote in message news:3F486C58.C9FC7929@chello.at...
>
>
> "Carlos Santander B." wrote:
> >
> > This code:
> >
> > class A { void a(float b) { printf("%f\n",b); } }
> > class B:A { void a(int b) { printf("%d\n",b); } }
> > void main() {
> >     B b=new B;
> >     b.a(3);
> >     b.a(4.5);
> > }
> >
> > Outputs
> > 3
> > 4
> >
> > To print correctly 4.5 I need to do b.A.a(4.5), which I personally find unatractive. So why, just like C++, doesn't D support polymorphism into derived classes?
>
> I think allowing this would at least also require a way to block methods from parent classes.
>
> On the other hand I think you could write
>
>  class B:A {
>    void a(int b) { printf("%d\n",b); }
>    void a(float b) { super.a(b); }
>  }
>
> easily.

I believe that walter has added `alias A.a a;` to solve this,
I still think that all non overrided methods from the super class should be
added into the sub class's search list.
void a( float b ) { super.a(b); } why ?? that rewrites a vtbl entry that's
already there and does the right thing.
can someone give a good example of why an overloaded methods show hide the
not overrriden superclasses methods. (passing the object as a sub class
would calls the right code!)

as in  (it should be noted that I changed 4.5 to 4.5f (4.5 is a double) so
has no direct match
import c.stdio;

class A {
    void a(float b) { printf("A::a(f)%f\n",b); }
    void a(int b) { printf("A::a(i) %d\n",b); }
}
class B:A {
    void a(int b) { printf("B::a(i) %d\n",b); }
}
void func(A a ) {
    a.a(3);
    a.a(4.5f);
}
void main() {
    B b=new B;
    printf("B ---");
    b.a(3);
    b.a(4.5f);
    printf("cast(A)B ---");
    func( b );
}
----------- 
B ---B::a(i) 3
B::a(i) 4
cast(A)B ---B::a(i) 3
A::a(f)4.500000

as you would expect.