www.digitalmars.com         C & C++   DMDScript  

D - Is it a normal behaviour ?

class A {}
class B : A {}
class C : B {}

void foo( A a ) { printf("A");}
void foo( B b ) { printf("B");}
void foo( C c ) { printf("C");}

void main() {
  A a = new C();
  foo( a );
}

It outputs "A"

Im wondering if it cannot be improved: all possible subclasses of A are
known, so can the compiler find out wich implementation of foo suits better
and replace foo( a ) by something like :

switch( a.class ) {
    case A : foo( (A)a );
    case B : foo( (B)a );
    case C : foo( (C)a );
}

Just a thougth ( probably a stupid one ).

-- Nicolas Repiquet
Aug 27 2003