D Beginner Trying Manual Memory Management

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jan 12 11:42:03 PST 2015


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).
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150112/02ae17f7/attachment.sig>


More information about the Digitalmars-d-learn mailing list