What is the design reasons for Not considering base class overloaded function?

Matthew Ong ongbp at yahoo.com
Tue May 24 22:28:48 PDT 2011


Hi,

file:///D:/JDKs/D/dmd2/html/d/function.html

Virtual Functions

 >All non-static non-private non-template member functions are virtual.
Not a problem here. This is to allow 
(global/structure/class/Interface)functions to be overridden by child 
class unlesss (final/sealed).

This may sound inefficient, but since the D compiler knows all of the 
class hierarchy when generating code, all functions that are not 
overridden can be optimized to be non-virtual.
Ok. Not an issue also. There are ways for compiler to trace the actual 
object being used

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:
...

However, when doing overload resolution, the functions in the base class 
are not considered:
...
                 // ### Why Not? Since B is a subset of A, and within B 
itself methods inherited from A can be called.
   b.foo(1);	// calls B.foo(long), since A.foo(int) not considered
...
To consider the base class's functions in the overload resolution 
process, use an AliasDeclaration:
...
alias A.foo foo;  // ### Why the extra steps is needed for the compiler 
to 'know' overloaded functions from base classes?
...
     B b = new B();
     b.foo(1);	// calls A.foo(int)
------------------------------------------------------------------
// ### most developer would expect that to be the case if >All 
non-static non-private non-template member functions are virtual.<
This seems to be self contradicting syntax. Even in scripting like bash 
and even the old awk, the path resolutions of a global functions uses 
the most newer/lasted defined automatically.
------------------------------------------------------------------

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: ?
What is the default encapsulations of a class functions? 
(public/private/package...)
I assume that the example shown here are public because they are used in 
function bar?

How about when the inheritance tree becomes deeper than 4? And more and 
more overloaded functions are in different classes? Does it mean we have 
to do more alias at child class at the bottom?

If I am not mistaken, in C++/Java. They will choose the most bottom up 
closes parameter type signature match. Even in scripting

All this questions are meant to understand what is the rational behind 
such design syntax. There is no description in those pages.

How about interface function defination inheritance?? Aikkk....
I do not expect interface should be deeply inherited.

-- 
Matthew Ong
email: ongbp at yahoo.com



More information about the Digitalmars-d-learn mailing list