How to override function?

grauzone none at example.net
Thu Oct 22 12:09:54 PDT 2009


Zarathustra wrote:
> I would like to know, how to override function if subclass and super class are located in the same package and different modules.
> 
> For instance when they are in the same module:
> //___________________________________
> module main;
> class Foo{
> 
>   this(){
>     proc();
>   }
>   
>   void proc(){
>     writefln("Foo");
>   }
> }
> 
> class Bar : Foo{
>   override void proc(){
>     writefln("Bar");
>   }
> }
> 
> void main(){
>   new Bar;
> }
> //___________________________________
> the result is "Bar" so it's great, but:
> 
> //___________________________________
> module pack.foo;
> 
> class Foo{
> 
>   this(){
>     proc();
>   }
>   
>   package void proc(){ // without 'package' it works well
>     writefln("Foo");
>   }
> }
> 
> //___________________________________
> module pack.bar;
> 
> class Bar : Foo{
>   package override void proc(){ // without 'package' it works well
>     writefln("Bar");
>   }
> }
> //___________________________________
> module main;
> void main(){
>   new Bar;
> }
> //___________________________________
> the result is "Foo" so it's unexpected to me.

It's a bug. package functions are never virtual, and the "override" 
attribute is just ignored. Same with private. Welcome to D.


More information about the Digitalmars-d-learn mailing list