is there any way for an object to make it self no longer usable
Frustrated via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jul 19 10:28:12 PDT 2014
On Saturday, 19 July 2014 at 12:02:50 UTC, Sean Campbell wrote:
> is there any way for an object to make it self no longer
> usable? eg
>
> class someclass {
> string somevalue;
> bool someflag;
> int somelength;
> this (value,length,flag) {
> somevalue = value;
> someflag = flag;
> somelength = length;
> }
> void modify_value(string new_value){
> somevalue = new_value;
> }
> void finalize_data(){
> //do something with data
> //make this invalid
> // eg delete this or this = null
> }
> }
>
> I don't want to use a private flag to tell weather the object
> is valid or not
You do realize that if you do this then anywhere in your code
you'll have to check if the object is valid before use? If the
objects lifetime is "random"(because the object itself can decide
when to destroy itself), then there is no way to know when it
will be destroyed.
If you do this you are potentially asking for a lot of access
violation errors or undefined behavior.
In any case, an easy way is to allow the object to allocate and
deallocate itself.
If the object knows the ptr and size that was used to allocate
itself it is trivial to deallocate itself.
http://dlang.org/phobos/core_memory.html
More information about the Digitalmars-d-learn
mailing list