www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Overloaded function disappears on polymorphism

reply "tcak" <tcak gmail.com> writes:
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
next sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
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=3D
 class 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
prev sibling next sibling parent "Tobias Pankrath" <tobias pankrath.net> writes:
 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
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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