Bug 4651 – Docs: Returned classes that have access to stack variables of its enclosing function
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dlang.org
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2010-08-15T19:23:00Z
Last change time
2012-01-23T23:30:43Z
Keywords
spec
Assigned to
nobody
Creator
andrej.mitrovich
Comments
Comment #0 by andrej.mitrovich — 2010-08-15T19:23:25Z
This is an outdated statement:
"While a non-static nested class can access the stack variables of its enclosing function, that access becomes invalid once the enclosing function exits:
class Base
{
int foo() { return 1; }
}
Base func()
{ int m = 3;
class Nested : Base
{
int foo() { return m; }
}
Base b = new Nested;
assert(b.foo() == 3); // Ok, func() is still active
return b;
}
int test()
{
Base b = func();
return b.foo(); // Error, func().m is undefined
}
"
As is stated in TDPL, variables in the local function get allocated on the heap if DMD detects that a returning class needs access to it. This section needs to be updated.
The next section with the code example for a workaround can be removed:
"If this kind of functionality is needed, the way to make it work is to make copies of the needed variables within the nested class's constructor"
Comment #1 by github-bugzilla — 2012-01-23T21:43:00Z