RAII and Deterministic Destruction
ZombineDev via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Aug 25 18:09:12 PDT 2015
On Tuesday, 25 August 2015 at 22:35:57 UTC, Jim Hewes wrote:
> Although C++ can be ugly, one reason I keep going back to it
> rather then commit more time to reference-based languages like
> C# is because I like deterministic destruction so much. My
> question is whether D can REALLY handle this or not. I've not
> been sure about this for some time so now I'm just going to
> come out and finally ask.
>
> I know about this RAII section in the documentation:
> http://dlang.org/cpptod.html#raii
> But I don't believe that handles all cases, such as having
> classes as member variables of other classes. (Do the members
> get destructors called too?)
>
> Then there is std.typecons.Unique and std.typecons.RefCounted.
> With these, can I really get deterministic destruction for all
> cases like I would in C++?
>
> If so, it might be a good idea to emphasize this more in the
> documentation because I'd think people coming from C++ would be
> looking for this.
>
> Jim
Structs in D behave like in C++ - they are created on the stack
(unless you use new or malloc), passed by value and are
automatically destroyed at the end of the scope. All their
members are destroyed recursively after the user defined
destructor is called.
Just try it out in a toy program and see if it works as you
expect.
More information about the Digitalmars-d-learn
mailing list