digitalmars.D.learn - override and package
- Namespace (55/55) Sep 12 2013 Code:
- Namespace (1/1) Sep 12 2013 Same with private, of course.
- Jacob Carlborg (27/31) Sep 12 2013 I think the error message is pretty clear. You cannot override a
- Namespace (3/36) Sep 12 2013 Obvious. But what happend? Is the original _apply hidden?
- Jacob Carlborg (6/7) Sep 12 2013 If you call through an object of the same type, T4 in this case, the
- Jacob Carlborg (5/8) Sep 12 2013 I'm guessing it's the same as overriding non-virtual methods in C++, if
- Maxim Fomin (9/21) Sep 12 2013 Actually error message is misleading - it complains as T1._apply
- Namespace (3/27) Sep 12 2013 Thank you, you understand my problem. ;)
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.
Sep 12 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
Sep 12 2013
On Thursday, 12 September 2013 at 11:29:22 UTC, Jacob Carlborg wrote:On 2013-09-12 11:28, Namespace wrote:Obvious. But what happend? Is the original _apply hidden?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" }
Sep 12 2013
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
Sep 12 2013
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
Sep 12 2013
On Thursday, 12 September 2013 at 11:29:22 UTC, Jacob Carlborg wrote:On 2013-09-12 11:28, Namespace wrote: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"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.
Sep 12 2013
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:Thank you, you understand my problem. ;) I agree, the message should be changed.On 2013-09-12 11:28, Namespace wrote: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"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.
Sep 12 2013









"Namespace" <rswhite4 googlemail.com> 