Function Inheritance and Overriding and Function Overloading In D
来源:互联网 发布:网络通信协议报文 编辑:程序博客网 时间:2024/06/03 22:56
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.
- Function Inheritance and Overriding and Function Overloading In D
- Function overloading and const keyword
- Name Mangling and Function Overloading
- C++ Overloading (Operator and Function)
- Overloading And Overriding
- Comparison between overloading operators in member function and in friend function
- Method of Overriding and Overloading
- Function overloading in C++
- Function in loop and closure
- Multi-Language Programming : Entity Object Inheritance And Function Overloading_3
- Function Overloading
- Function overloading
- 482 D. Random Function and Tree
- 21.重载与重写(Overloading and Overriding Are Different)
- 6.Overloading, Overriding, Runtime Type and Object Orientation (inner)
- Function Pointers and Callbacks in C
- round() Function in Python and Matlab
- Lexical scope and function closures in R
- 2007年12月14日收盘后小结.
- 配置出错解决方法
- 迷宫问题递归算法
- 試用期間社員自己評価表(記念として)
- Unified Communications
- Function Inheritance and Overriding and Function Overloading In D
- 先按ctrl再按A和先按A再按ctrl一样吗?
- 请问Bind ,Eval有说明区别
- ASP.NET-用户'NT AUTHORITY/NETWORK SERVICE' 登录失败的解决方法
- 按下Ctrl弹出一个对话框松开关闭此对话框,如何实现?
- vc开发之感受
- asp.net页面从初始化到卸载事件顺序(及对页面控件调用)完整列表
- Ajax中的UpdatePanel与Freetextbox兼容问题的初步解决方法
- ASP.NET-ASP.NET配置(web.config)无法连接到SQL Server 数据库的解决方法