override and package

Jacob Carlborg doob at me.com
Thu Sep 12 04:29:21 PDT 2013


On 2013-09-12 11:28, Namespace wrote:

> But if I try to write 'override' before [1], I get this error message:
> Error: function T4._apply cannot override a non-virtual function
>
> This seems inconsistent. I really overwrite the method, and then I put
> it in a package label.

I think the error message is pretty clear. You cannot override a 
function that isn't virtual. Private and package methods are not 
virtual. Example:

class Base
{
     void foo ()
     {
         writeln("Base.foo");
     }
}

class Sub : Base
{
     package void foo ()
     {
         writeln("Sub.foo");
     }
}

void main ()
{
     auto sub = new Sub;
     sub.foo(); // prints "Sub.foo" as expected

     Base base = sub;
     base.foo(); // prints "Base.foo"
}

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list