Scope statement
Lars V
nospamplease at nospam.com
Tue Feb 5 15:26:50 PST 2008
Bill Baxter Wrote:
> Lars V wrote:
> > Hi,
> >
> > I have a question:
> >
> > void foo()
> > {
> > Obj o = new Obj();
> > }
> >
> > In this code, the destructor of the object will be called ALWAYS at the end of the function?
> >
> > In D, the objects are not destroyed when they go out of their scopes??? Is necessary to use the "scope" statement?
> >
> > I really don't understand the necessity of the scope statement... could anybody explain it?
>
> Yes you need to use scope if you want deterministic destruction.
>
> Allocation and deallocation are expensive, and one of the nice things
> about a garbage collection system is that it lets you do clean-up more
> lazily. That way you can handle a bunch of de-allocations at once.
> Think of it as waiting a week to vacuum up the room instead of pulling
> out the vacuum cleaner every time a piece of dust hits the floor.
>
> --bb
But, when the function ends the object should be collected by the GC (there are no more references to it) and the GC will call the destructor, no? I don't understand it.
More information about the Digitalmars-d-learn
mailing list