November 13, 2004 function overload + function override = compiler error | ||||
---|---|---|---|---|
| ||||
Hi, In the following program, there is a base class with an accessor and a modifier overloading the same name, to function like a class `property'. There are two classes derived from the base, one is trivial, and the other overrides the base modifier. The former compiles fine, whereas the latter causes the compiler to think that there is no base accessor: see embedded comment below. Use -version=BROKEN or -fversion=BROKEN on the command line to observe the compiler error. The language documentation says: `In D, function overloading is simple. It matches exactly, it matches with implicit conversions, or it does not match. If there is more than one match, it is an error.' I don't think the above is the cause of the problem, but I'm only two D-weeks old and I may be wrong. Am I? Thanks, Jay -------------------------------------- class Base { public: char[] prop() { return _prop; } void prop(char[] v) { _prop = v; } private: char[] _prop; } class Derived1 : public Base { public: void print() { printf("Derived1.prop = %*s\n", prop); printf("Derived1.prop() = %*s\n", prop()); } } class Derived2 : public Base { public: override void prop(char[] v) { super.prop = v; if (_prop == "No17") { printf("Special case\n"); } } void print() { version (BROKEN) { // The following two uses of `prop' should refer to // Base.prop(), shouldn't they? The compiler wants // them to be Derived2.prop(char[]). printf("Derived2.prop = %*s\n", prop); printf("Derived2.prop() = %*s\n", prop()); } else { printf("Derived2.super.prop = %*s\n", super.prop); } } } int main() { Derived1 d1 = new Derived1; Derived2 d2 = new Derived2; d1.prop = "No17"; d1.prop = "No16"; d1.print(); d2.prop = "No17"; d2.prop = "No16"; d2.print(); return 0; } |
November 13, 2004 Re: function overload + function override = compiler error | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jay | In article <cn4m29$dna$1@digitaldaemon.com>, Jay says... > >Hi, > >In the following program, there is a base class with an accessor and a modifier overloading the same name, to function like a class `property'. There are two classes derived from the base, one is trivial, and the other overrides the base modifier. The former compiles fine, whereas the latter causes the compiler to think that there is no base accessor: see embedded comment below. This is correct behavior. It has to do with the symbol lookup rules. To get everything working as you want, add an alias for the accessor in the derived class. Sean |
Copyright © 1999-2021 by the D Language Foundation