Beginner: How does an object delete itself?

Tim M a at b.com
Fri Oct 3 03:06:40 PDT 2008


On Mon, 22 Sep 2008 03:15:22 +1200, nobody <somebody at somewhere.com> wrote:

> Hello. I just started using OOP and had a question.
>
> I have something like this:
>
> class Foo{
> ..
>     private void deleteMyself()
>     {
>         this.delete;
>         foo.length--;
>     }
> ..
> }
>
> Foo[] foo;
>
> But this obviously doesn't work.
> The problem is, how do I know what position in the array the object is,  
> and
> how do I delete it?
> I don't want empty elements in my array.
>
>


By now I assume you understand that was a bad idea but the replies don't  
seem to go into much detail why. If you have one process and that process  
is only making use of the initial thread, no other threads are cretaed.  
Then when this method gets called to delete self and the object is  
deleted, where doe's the next line of code execution occur?

A way to make something like that work would be to have each running in  
there own thread and then they delete there own thread. If no other  
threads have a reference to the same object then the garbage collecor  
cleans up for you. Another way is provide your own delete self which just  
removes references to itself from other classes and the gc will then clean  
it up again.



More information about the Digitalmars-d-learn mailing list