example of pointer usefulness in D

Brad Anderson via Digitalmars-d digitalmars-d at puremagic.com
Tue Oct 21 10:00:31 PDT 2014


On Tuesday, 21 October 2014 at 16:55:09 UTC, Jeremy DeHaan wrote:
> On Tuesday, 21 October 2014 at 12:40:57 UTC, w0rp wrote:
>> On Tuesday, 21 October 2014 at 12:22:54 UTC, edn wrote:
>>> Could someone provide me with examples showing the usefulness 
>>> of pointers in the D language? They don't seem to be used as 
>>> much as in C and C++.
>>
>> You can use C libraries in D, and pointers will surely come 
>> into regular use there. You could also heap allocate structs 
>> and end up with pointers to structs.
>
>
> Can you do the same with classes if you wanted to avoid a GC
> allocation? Just wondering.

You can create a class in any chunk of memory (say memory 
returned through malloc for instance) using std.conv.emplace: 
http://dlang.org/library/std/conv/emplace.html

std.typecons.Unique and std.typecons.Scoped are better options if 
you are going for a scope destroyed class or a class on the stack 
as they take care of the details for you. std.typecons.RefCounted 
would be a great option but it does not yet work on classes.


More information about the Digitalmars-d mailing list