How do you make a copy TO and object when you're INSIDE of it?

Enjoys Math via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 23 20:27:11 PDT 2015


On Friday, 24 July 2015 at 03:12:43 UTC, Steven Schveighoffer 
wrote:
> On 7/23/15 9:30 PM, Enjoys Math wrote:
>>[...]
>
> You're approaching this wrong. Do the lookup before deciding 
> whether to instantiate a new object:
>
> static Grammar getGrammar(const T[] str) {
>      if(auto x = str in grammarCache)
>          return *x;
>      else
>      {
>          auto g = new Grammar;
>          grammarCache[str] = g;
>          return g;
>      }
> }
>
> If you always want to dup, then do it outside the lookup. Don't 
> do it in the constructor, you already have an object by then.
>
> -Steve

Thanks.  That sounds like a good approach


More information about the Digitalmars-d-learn mailing list