D Beginner Trying Manual Memory Management
jmh530 via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Jan 12 12:14:19 PST 2015
Thanks for the reply, I wasn't familiar with scoped. I was aware
that structs are on the stack and classes are on the heap in D,
but I didn't know it was possible to put a class on the stack.
Might be interesting to see how this is implemented.
After looking up some more C++, I think what I was trying to do
is more like make_unique than unique_ptr.
On Monday, 12 January 2015 at 19:42:14 UTC, ketmar via
Digitalmars-d-learn wrote:
> On Mon, 12 Jan 2015 19:29:53 +0000
> jmh530 via Digitalmars-d-learn
> <digitalmars-d-learn at puremagic.com>
> wrote:
>
> the proper answer is too long to write (it will be more an
> article that
> a forum answer ;-), so i'll just give you some directions:
>
> import std.typecons;
>
> {
> auto b = scoped!B(); // `auto` is important here!
> ...
> }
>
> `scoped!` allocating class instance *on* *stack*, and
> automatically
> calls destructor when object goes out of scope.
>
> but you'd better consider using struct for such things, as
> struct are
> stack-allocated by default (unlike classes, which are reference
> type
> and should be allocated manually).
>
> there is a big difference between `class` and `struct` in D,
> much
> bigger that in C++ (where it's only about default protection,
> actually).
More information about the Digitalmars-d-learn
mailing list