Function Inheritance and Overriding and Function Overloading In D

来源:互联网 发布:网络通信协议报文 编辑:程序博客网 时间:2024/06/03 22:56
Function Inheritance and Overriding
A functions in a derived class with the same name and parameter types as a function in a base class overrides that function:
class A
{
int foo(int x) { ... }
}

 

class B : A
{
override int foo(int x) { ... }
}

void test()
{
B b = new B();
bar(b);
}

void bar(A a)
{
a.foo(1); // calls B.foo(int)
}
However, when doing overload resolution, the functions in the base class are not considered:

class A
{
int foo(int x) { ... }
int foo(long y) { ... }
}

class B : A
{
override int foo(long x) { ... }
}

void test()
{
B b = new B();
b.foo(1); // calls B.foo(long), since A.foo(int) not considered
A a = b;
a.foo(1); // issues runtime error (instead of calling A.foo(int))
}
To consider the base class's functions in the overload resolution process, use an AliasDeclaration:

class A
{
int foo(int x) { ... }
int foo(long y) { ... }
}

class B : A
{
alias A.foo foo;
override int foo(long x) { ... }
}

void test()
{
B b = new B();
bar(b);
}

void bar(A a)
{
a.foo(1); // calls A.foo(int)
B b = new B();
b.foo(1); // calls A.foo(int)
}
If such an AliasDeclaration is not used, the derived class's functions completely override all the functions of the same name in the base class, even if the types of the parameters in the base class functions are different. If, through implicit conversions to the base class, those other functions do get called, an std.HiddenFuncError exception is raised:

import std.hiddenfunc;

class A
{
void set(long i) { }
void set(int i) { }
}
class B : A
{
void set(long i) { }
}

void foo(A a)
{ int i;
try
{
a.set(3); // error, throws runtime exception since
// A.set(int) should not be available from B
}
catch (HiddenFuncError o)
{
i = 1;
}
assert(i == 1);
}

void main()
{
foo(new B);
}
If an HiddenFuncError exception is thrown in your program, the use of overloads and overrides needs to be reexamined in the relevant classes.

A function parameter's default value is not inherited:

class A
{
void foo(int x = 5) { ... }
}

class B : A
{
void foo(int x = 7) { ... }
}

class C : B
{
void foo(int x) { ... }
}

void test()
{
A a = new A();
a.foo(); // calls A.foo(5)

B b = new B();
b.foo(); // calls B.foo(7)

C c = new C();
c.foo(); // error, need an argument for C.foo
}

Function Overloading
In C++, there are many complex levels of function overloading, with some defined as "better" matches than others. If the code designer takes advantage of the more subtle behaviors of overload function selection, the code can become difficult to maintain. Not only will it take a C++ expert to understand why one function is selected over another, but different C++ compilers can implement this tricky feature differently, producing subtly disastrous results.

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.

Functions defined with non-D linkage cannot be overloaded.