digitalmars.D.learn - Overloaded function disappears on polymorphism
- tcak (21/21) Jan 24 2015 main.d
- ketmar (6/30) Jan 24 2015 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D...
- Tobias Pankrath (2/5) Jan 24 2015 alias makeBeep = Car.makeBeep; pulls the base classes overloads
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (46/66) Jan 24 2015 This is called "name hiding" and works the same at least in C++. The
main.d =================================================== class Car{ public void makeBeep( char c ){} public void makeBeep( string s ){} } class Tesla: Car{ override public void makeBeep( char c ){ writeln("C = ", c); } } void main(){ auto t = new Tesla(); t.makeBeep("Model S"); } =================================================== Error: function main.Tesla.makeBeep (char c) is not callable using argument types (string) For Tesla, I expected that makeBeep( string s ) would still be defined. Because I have overridden on makeBeep( char c ). Isn't that correct?
Jan 24 2015
On Sat, 24 Jan 2015 18:55:58 +0000, tcak wrote:main.d =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D==3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3Dclass Car{ public void makeBeep( char c ){} public void makeBeep( string s ){} } =20 class Tesla: Car{ override public void makeBeep( char c ){ writeln("C =3D ", c); } } =20 void main(){ auto t =3D new Tesla(); =20 t.makeBeep("Model S"); } =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D==3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=20 Error: function main.Tesla.makeBeep (char c) is not callable using argument types (string) =20 For Tesla, I expected that makeBeep( string s ) would still be defined. Because I have overridden on makeBeep( char c ). Isn't that correct?nope. overriding overloaded function cancels all overloads. it's by spec.=
Jan 24 2015
For Tesla, I expected that makeBeep( string s ) would still be defined. Because I have overridden on makeBeep( char c ). Isn't that correct?alias makeBeep = Car.makeBeep; pulls the base classes overloads into your subclass.
Jan 24 2015
On 01/24/2015 10:55 AM, tcak wrote:main.d =================================================== class Car{ public void makeBeep( char c ){} public void makeBeep( string s ){} } class Tesla: Car{ override public void makeBeep( char c ){ writeln("C = ", c); } } void main(){ auto t = new Tesla(); t.makeBeep("Model S"); } =================================================== Error: function main.Tesla.makeBeep (char c) is not callable using argument types (string) For Tesla, I expected that makeBeep( string s ) would still be defined. Because I have overridden on makeBeep( char c ). Isn't that correct?This is called "name hiding" and works the same at least in C++. The purpose is to avoiding function hijacking. Imagine the following scenario where a 'short' argument is dispatched to a function taking 'int': class Base { void foo(int) {} } class Derived : Base { override void foo(int) {} } void main() { // Compiles: 'short' is converted to 'int': (new Derived).foo(short.init); } Without name hiding, the following addition to Base (which, presumably the author of Derived has no control over) would cause the call to bypass Derived.foo(short) and go to Base.foo(short): class Base { void foo(int) {} void foo(short) // <-- New function {} } class Derived : Base { override void foo(int) {} } void main() { /* Compilation ERROR: * * Deprecation: class deneme.Derived use of deneme.Base.foo(short * _param_0) hidden by Derived is deprecated; use 'alias foo = Base.foo;' * to introduce base class overload set */ (new Derived).foo(short.init); } Ali
Jan 24 2015