www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Problem of override

reply "Eric Suen" <eric.suen.tech gmail.com> writes:
Why these code is valid:

class A {
  public char[] test() {
    return "A";
  }
}

class B : A {
}

class C : B {
  public char[] test() {
    return super.test() ~ " C";
  }
}

But these are not?

class A {
  public char[] test() {
    return "A";
  }
}

class B : A {
  public void test(char[] a) {
  }
}

class C : B {
  public char[] test() {
    return super.test() ~ " C";
  }
} 
Sep 29 2007
parent reply Frank Benoit <keinfarbton googlemail.com> writes:
Does this help?

Eric Suen schrieb:
 
 class A {
   public char[] test() {
     return "A";
   }
 }
 
 class B : A {
alias A.test test;
   public void test(char[] a) {
   }
 }
 
 class C : B {
   public char[] test() {
     return super.test() ~ " C";
   }
 } 
Sep 29 2007
parent reply "Eric Suen" <eric.suen.tech gmail.com> writes:
Is this the bug of D compiler or the feature of
D language? this will be a big problem for visitor pattern,
for examples, Parser, like:

visit(IfStatement s);
visit(WhileStatement s);
....

So if I want override some of the method, how? that is really
weird, Java doesn't has this problem...

Eric

"Frank Benoit" <keinfarbton googlemail.com>
 Does this help?

 Eric Suen schrieb:
 class A {
   public char[] test() {
     return "A";
   }
 }

 class B : A {
alias A.test test;
   public void test(char[] a) {
   }
 }

 class C : B {
   public char[] test() {
     return super.test() ~ " C";
   }
 } 
Sep 29 2007
parent Frank Benoit <keinfarbton googlemail.com> writes:
Yes, it is not bug it is a feature. :(

From http://www.digitalmars.com/d/1.0/function.html
"However, when doing overload resolution, the functions in the base
class are not considered:"
"To consider the base class's functions in the overload resolution
process, use an AliasDeclaration:"

Also see my postings "override and overload" 13-Nov-06
and "aliasing base methods" from 25-Feb-07


Eric Suen schrieb:
 Is this the bug of D compiler or the feature of
 D language? this will be a big problem for visitor pattern,
 for examples, Parser, like:
 
 visit(IfStatement s);
 visit(WhileStatement s);
 .....
 
 So if I want override some of the method, how? that is really
 weird, Java doesn't has this problem...
 
 Eric
 
 "Frank Benoit" <keinfarbton googlemail.com>
 Does this help?

 Eric Suen schrieb:
 class A {
   public char[] test() {
     return "A";
   }
 }

 class B : A {
alias A.test test;
   public void test(char[] a) {
   }
 }

 class C : B {
   public char[] test() {
     return super.test() ~ " C";
   }
 } 
Sep 29 2007