www.digitalmars.com         C & C++   DMDScript  

D - polymorphism into derived classes

reply "Carlos Santander B." <carlos8294 msn.com> writes:
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
Aug 23 2003
parent reply Helmut Leitner <helmut.leitner chello.at> writes:
"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
Aug 24 2003
parent "Mike Wynn" <mike.wynn l8night.co.uk> writes:
"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.
Aug 24 2003