`
lifaming15
  • 浏览: 64846 次
  • 来自: ...
文章分类
社区版块
存档分类

Function Inheritance and Overriding and Function Overloading In D

 
阅读更多
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.

分享到:
评论

相关推荐

    Problem Solving with C++ (7th edition)

    - **Virtual Functions**: Discussion of virtual functions and their role in achieving polymorphism through inheritance. **Programming Project 15.12**: This project likely involves designing and ...

    Lerner -- Python Workout. 50 Essential Exercises -- 2020.pdf

    - **Objective:** Write a function that takes a list of numbers and returns their sum. - **Key Concepts:** - Looping over elements in a list. - Using the `sum()` built-in function. - Handling ...

    InheritanceAndPolymorphism

    2. **单一继承与多重继承(Single and Multiple Inheritance)**: Java支持单一继承,即一个子类只能继承一个父类。这有助于避免因多重继承可能导致的二义性问题。然而,通过接口(interfaces),Java可以实现类似...

    多型与虚拟Polymorphism in C++

    1. 函数重载(Function Overloading) 函数重载是指在同一作用域内,可以有多个同名函数,但它们的参数列表不同,即参数类型、个数或顺序不一致。这使得开发者可以根据传入的不同参数调用合适的函数,实现了同名函数...

    关于C#、.NET的常用面试题

    5. 多态(Polymorphism):同一种行为在不同类中有不同的实现,如方法重载(Overloading)和方法重写(Overriding)。 三、.NET框架 1. .NET Framework概述:它是一个全面的开发平台,包括公共语言运行库(CLR)和...

    全面解析PHP面向对象的三大特征

    1. **继承(Inheritance)** - **实现方式**:在PHP中,使用`extends`关键字可以让一个类继承另一个类。例如,`class Student extends Person{}`表示`Student`类继承了`Person`类。 - **注意事项**: - 子类只能...

    JavaScript 继承 封装 多态实现及原理详解

    3. 多态:在JavaScript中,可以通过函数重写(function overriding)或者条件判断,结合继承机制来实现方法的多态。通过将具有相同功能的方法在子类中重新实现,以适应不同子类的具体需求。 JavaScript中的继承封装...

    JAVA学习笔记必看经典四.pdf

    - **方法重载(Overloading)**:在同一个类中,方法名相同,但参数列表不同的多个方法构成重载。如`teach() {}`和`teach(int a) {}`是重载方法。 - **方法覆盖(Overriding)**:子类提供特定实现的父类方法。`...

    java代码-第三章,习题3,6.

    - 函数(Function),也称为方法(Method),包括定义(Definition)和调用(Calling) - 构造器(Constructor)的作用和特性 - 方法重载(Method Overloading)的概念 - 封装(Encapsulation)原则,包括私有...

Global site tag (gtag.js) - Google Analytics