[Issue 279] New: Nested class can't access var in outer function scope, if nested in class
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Aug 5 12:19:18 PDT 2006
http://d.puremagic.com/issues/show_bug.cgi?id=279
Summary: Nested class can't access var in outer function scope,
if nested in class
Product: D
Version: 0.163
Platform: PC
OS/Version: Linux
Status: NEW
Keywords: wrong-code
Severity: normal
Priority: P2
Component: DMD
AssignedTo: bugzilla at digitalmars.com
ReportedBy: web at kwi.dk
The nested class can refer to members of the enclosing class, but not variables
in the enclosing function.
I assume the problem is that the context pointer of the anonymous class nested
within Foo's constructor refers to the Foo instance, and not to the stack-frame
of the constructor.
Whether intentional or not, the compiler accepts the program and generates
buggy code.
--- Test case ---
import std.stdio;
void foo()
{
int x = 42;
new class Object
{
this() { writef("%s\n", x); }
};
}
class Foo
{
this()
{
int x = 42;
new class Object
{
this() { writef("%s\n", x); }
// Can't access 'x'. Any members of Foo are accessible, however.
};
}
}
void main()
{
foo(); // prints 42
new Foo(); // prints garbage integer (e.g. 0, on my system.)
}
--
More information about the Digitalmars-d-bugs
mailing list