Is there a weak pointer or references in D?

Era Scarecrow rtcvb32 at yahoo.com
Fri Jan 11 13:12:39 PST 2013


On Friday, 11 January 2013 at 18:22:30 UTC, Charles Hixson wrote:
> I was looking for a way to create a weak reference to either a 
> struct or a class.  I need to be able to use it to 
> automatically generate an active reference on access.  (I 
> intend to do this by rolling in data from a file.)
>
> Any guidance as to where I should look?

  Looks like you'll have to create the access yourself, you can 
forward access to the new ptr via a property call by the function 
you name. This works.

   struct S {
     int x;
     S* _s;  //for weak ptr.

     //constructor details so we can show it's
     //differences in the lower prints.
     S* s(int x) {
       if (_s is null)
         _s = new S(x);

       return _s;
     }

     void print(){writeln(this);}
   }

   S test;
   test.print();
   test.s(10).print();
   test.print();


  I've tried making a weakPtr template function, however it 
complains about a nested function call (perhaps cause it was 
within a unittest); Shows the problem(s) on that side.

  It's possible to write a mixin that could do those details for 
you.


More information about the Digitalmars-d-learn mailing list