Local static class fields

Simen Kjærås simen.kjaras at gmail.com
Tue Aug 13 08:40:59 UTC 2019

On Tuesday, 13 August 2019 at 04:43:29 UTC, Paul Backus wrote:
> On Monday, 12 August 2019 at 22:48:43 UTC, Bert wrote:
>> Making a field static is effectively a global variable to the 
>> class.
>> I have a recursive class structure(think of a graph or tree) 
>> and I need to keep a global state for it, but this state 
>> actually needs to be different for each tree object. The 
>> reason for this is that structurally it will not change per 
>> tree and if it it is not static then it wastes space 
>> unnecessarily.
>> [...]
>> Is there any way to do this more naturally in D?
> It seems to me like the obvious solution is to use two 
> different classes, one to store the global state, and one to 
> store the individual objects in your structure. For example:
> class Tree {
>     State state;
>     Node root;
> }
> class Node {
>     Node[] children;
> }

So I have a Node. How do I find the Tree it belongs to?


More information about the Digitalmars-d-learn mailing list