Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
September 12, 2013 override and package | ||||
---|---|---|---|---|
| ||||
Code: ---- import std.stdio; class T1 { protected: void _apply() { writeln("Call T1"); } } class T2 : T1 { public: override void _apply() { writeln("Call T2"); } } class T3 : T1 { protected: override void _apply() { writeln("Call T3"); } } class T4 : T1 { package: void _apply() { /// <-- [1] writeln("Call T4"); } } void main() { T1 t1 = new T1(); T2 t2 = new T2(); T3 t3 = new T3(); T4 t4 = new T4(); t1._apply(); t2._apply(); t3._apply(); t4._apply(); } ---- Produce the correct output: Call T1 Call T2 Call T3 Call T4 If I remove 'override' from T3 (or also T2) I get the correct deprecation message: /d172/f194.d(19): Deprecation: overriding base class function without using override attribute is deprecated (f194.T3._apply overrides f194.T1._apply) 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. |
September 12, 2013 Re: override and package | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | Same with private, of course. |
September 12, 2013 Re: override and package | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | 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 |
September 12, 2013 Re: override and package | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Thursday, 12 September 2013 at 11:29:22 UTC, Jacob Carlborg wrote:
> 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"
> }
Obvious. But what happend? Is the original _apply hidden?
|
September 12, 2013 Re: override and package | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | On 2013-09-12 13:34, Namespace wrote: > Obvious. But what happend? Is the original _apply hidden? If you call through an object of the same type, T4 in this case, the method in the base class is hidden. If you call through a base class reference, T1 in this case, the "_apply" method in the subclass is hidden. -- /Jacob Carlborg |
September 12, 2013 Re: override and package | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On 2013-09-12 13:39, Jacob Carlborg wrote: > If you call through an object of the same type, T4 in this case, the > method in the base class is hidden. If you call through a base class > reference, T1 in this case, the "_apply" method in the subclass is hidden. I'm guessing it's the same as overriding non-virtual methods in C++, if you're familiar with that. -- /Jacob Carlborg |
September 12, 2013 Re: override and package | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Thursday, 12 September 2013 at 11:29:22 UTC, Jacob Carlborg
wrote:
> 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.
Actually error message is misleading - it complains as T1._apply
would be non-virtual function which is wrong, it is one. The
problem is not in case of overriding non-virtual function but in
attempt to override virtual function (private) by non-virtual one
(package). I think error mesage should be something like:
"Error: function T4._apply is non-virtual function and cannot
ovverride virtual function T1._apply"
|
September 12, 2013 Re: override and package | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxim Fomin | On Thursday, 12 September 2013 at 16:16:14 UTC, Maxim Fomin wrote:
> On Thursday, 12 September 2013 at 11:29:22 UTC, Jacob Carlborg
> wrote:
>> 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.
>
> Actually error message is misleading - it complains as T1._apply
> would be non-virtual function which is wrong, it is one. The
> problem is not in case of overriding non-virtual function but in
> attempt to override virtual function (private) by non-virtual one
> (package). I think error mesage should be something like:
>
> "Error: function T4._apply is non-virtual function and cannot
> ovverride virtual function T1._apply"
Thank you, you understand my problem. ;)
I agree, the message should be changed.
|
Copyright © 1999-2021 by the D Language Foundation