[Issue 2999] New: Return-type overloading should be error
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sun May 17 06:12:49 PDT 2009
http://d.puremagic.com/issues/show_bug.cgi?id=2999
Summary: Return-type overloading should be error
Product: D
Version: 2.030
Platform: PC
OS/Version: All
Status: NEW
Keywords: accepts-invalid
Severity: normal
Priority: P2
Component: DMD
AssignedTo: bugzilla at digitalmars.com
ReportedBy: rsinfu at gmail.com
This code compiles without error:
--------------------
short foo() { return 1; }
int foo() { return 2; }
--------------------
Yes, the above example is not so serious because if you try to use foo, the
compiler will spit an error.
But it will be serious when function overriding is involved:
--------------------
interface I { short foo(); }
interface J { int foo(); }
class C : I, J
{
override short foo() { return 1; }
override int foo() { return 2; }
}
void main()
{
I i = new C;
J j = new C;
writeln(i.foo);
writeln(j.foo);
}
--------------------
The compiler silently accepts this code. And the result is:
--------------------
1
10289153
--------------------
Note that the lower 16-bit of 10289153 is 1. Thus, the short foo() is invoked.
If the declaration order of C.foo is reversed, the result becomes
--------------------
2
2
--------------------
In this case, int foo() is invoked.
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list