May 03, 2004
When happens when delete this is done inside a class?
Why doesn't it immediatelly call the destructor?

I wrote a class that is ment to be a temporary object return by functions and i would like it to be destructed right after it is used. So i tried:

class ref(Type)
{
 this(inout Type t)
 {
  stdout.writeLine("constructor " ~ string.toString(t));
  elem = &t;
 }
 ~this()
 {
  stdout.writeLine("destructor " ~ string.toString(*elem));
 }
 Type* elem;
 Type val(){return *elem; delete this; }
 Type val(Type t){(*elem)=t; return *elem; delete this;}
}

Inside val i have "delete this" but in this code:

<CODE>
class A
{
     void print()
     {
          stdout.writeLine("A[ x=" ~ std.string.toString(x)~"]");
     }
     ref!(int) returnbyrefx()
     {
          return new ref!(int)(x);
     }
    int x = 10;
}

int main ( char [] [] args )
{
     A a = new A();
     a.print();
     a.returnbyrefx.val = 7;
     a.print();
     getch();
     return 1;
}
</CODE>

i see that the destructor of ref is called when the program is exited.
So what does then "delete this" mean, does it only tell GC that
the object can be collected?


May 03, 2004
Sorry first sentence should have been:
What happens when delete this is done inside a class?

"Ivan Senji" <ivan.senji@public.srce.hr> wrote in message news:c756ct$1jsi$1@digitaldaemon.com...
> When happens when delete this is done inside a class?
> Why doesn't it immediatelly call the destructor?
>
> I wrote a class that is ment to be a temporary object return by functions and i would like it to be destructed right after it is used. So i tried:
>
> class ref(Type)
> {
>  this(inout Type t)
>  {
>   stdout.writeLine("constructor " ~ string.toString(t));
>   elem = &t;
>  }
>  ~this()
>  {
>   stdout.writeLine("destructor " ~ string.toString(*elem));
>  }
>  Type* elem;
>  Type val(){return *elem; delete this; }
>  Type val(Type t){(*elem)=t; return *elem; delete this;}
> }
>
> Inside val i have "delete this" but in this code:
>
> <CODE>
> class A
> {
>      void print()
>      {
>           stdout.writeLine("A[ x=" ~ std.string.toString(x)~"]");
>      }
>      ref!(int) returnbyrefx()
>      {
>           return new ref!(int)(x);
>      }
>     int x = 10;
> }
>
> int main ( char [] [] args )
> {
>      A a = new A();
>      a.print();
>      a.returnbyrefx.val = 7;
>      a.print();
>      getch();
>      return 1;
> }
> </CODE>
>
> i see that the destructor of ref is called when the program is exited.
> So what does then "delete this" mean, does it only tell GC that
> the object can be collected?
>
>


May 03, 2004
In article <c756j2$1k3h$1@digitaldaemon.com>, ivan.senji@public.srce.hr says...
>
>Sorry first sentence should have been:
>What happens when delete this is done inside a class?
>
>"Ivan Senji" <ivan.senji@public.srce.hr> wrote in message news:c756ct$1jsi$1@digitaldaemon.com...
>> When happens when delete this is done inside a class?
>> Why doesn't it immediatelly call the destructor?
>>
>> I wrote a class that is ment to be a temporary object return by functions and i would like it to be destructed right after it is used. So i tried:
>>
>> class ref(Type)
>> {
>>  this(inout Type t)
>>  {
>>   stdout.writeLine("constructor " ~ string.toString(t));
>>   elem = &t;
>>  }
>>  ~this()
>>  {
>>   stdout.writeLine("destructor " ~ string.toString(*elem));
>>  }
>>  Type* elem;
>>  Type val(){return *elem; delete this; }
>>  Type val(Type t){(*elem)=t; return *elem; delete this;}
>> }
>>
>> Inside val i have "delete this" but in this code:
>>
>> <CODE>
>> class A
>> {
>>      void print()
>>      {
>>           stdout.writeLine("A[ x=" ~ std.string.toString(x)~"]");
>>      }
>>      ref!(int) returnbyrefx()
>>      {
>>           return new ref!(int)(x);
>>      }
>>     int x = 10;
>> }
>>
>> int main ( char [] [] args )
>> {
>>      A a = new A();
>>      a.print();
>>      a.returnbyrefx.val = 7;
>>      a.print();
>>      getch();
>>      return 1;
>> }
>> </CODE>
>>
>> i see that the destructor of ref is called when the program is exited.
>> So what does then "delete this" mean, does it only tell GC that
>> the object can be collected?
>>
>>

The return statement causes execution to immediately jump back to the caller; the delete statements are not reached in your code. You need to use a temporary variable to hold the return value, then delete the object, and finally return the temp.


-- 
Christopher E. Miller

May 03, 2004
"Ivan Senji" <ivan.senji@public.srce.hr> wrote in message
news:c756ct$1jsi$1@digitaldaemon.com
| When happens when delete this is done inside a class?
| Why doesn't it immediatelly call the destructor?
|
| I wrote a class that is ment to be a temporary object
| return by functions and i would like it to be destructed
| right after it is used. So i tried:
|
| [...]
|
| i see that the destructor of ref is called when the program is exited.
| So what does then "delete this" mean, does it only tell GC that
| the object can be collected?

You may want to use auto objects or auto classes.

-----------------------
Carlos Santander Bernal


May 03, 2004
"Vathix" <vathixSpamFix@dprogramming.com> wrote in message news:c75a6q$1nsp$1@digitaldaemon.com...
> In article <c756j2$1k3h$1@digitaldaemon.com>, ivan.senji@public.srce.hr says...
> >
> >Sorry first sentence should have been:
> >What happens when delete this is done inside a class?
> >
> >"Ivan Senji" <ivan.senji@public.srce.hr> wrote in message news:c756ct$1jsi$1@digitaldaemon.com...
> >> When happens when delete this is done inside a class?
> >> Why doesn't it immediatelly call the destructor?
> >>
> >> I wrote a class that is ment to be a temporary object return by functions and i would like it to be destructed right after it is used. So i tried:
> >>
> >> class ref(Type)
> >> {
> >>  this(inout Type t)
> >>  {
> >>   stdout.writeLine("constructor " ~ string.toString(t));
> >>   elem = &t;
> >>  }
> >>  ~this()
> >>  {
> >>   stdout.writeLine("destructor " ~ string.toString(*elem));
> >>  }
> >>  Type* elem;
> >>  Type val(){return *elem; delete this; }
> >>  Type val(Type t){(*elem)=t; return *elem; delete this;}
> >> }
> >>
> >> Inside val i have "delete this" but in this code:
> >>
> >> <CODE>
> >> class A
> >> {
> >>      void print()
> >>      {
> >>           stdout.writeLine("A[ x=" ~ std.string.toString(x)~"]");
> >>      }
> >>      ref!(int) returnbyrefx()
> >>      {
> >>           return new ref!(int)(x);
> >>      }
> >>     int x = 10;
> >> }
> >>
> >> int main ( char [] [] args )
> >> {
> >>      A a = new A();
> >>      a.print();
> >>      a.returnbyrefx.val = 7;
> >>      a.print();
> >>      getch();
> >>      return 1;
> >> }
> >> </CODE>
> >>
> >> i see that the destructor of ref is called when the program is exited.
> >> So what does then "delete this" mean, does it only tell GC that
> >> the object can be collected?
> >>
> >>
>
> The return statement causes execution to immediately jump back to the caller; the delete statements are not reached in your code. You need to
use
> a temporary variable to hold the return value, then delete the object, and finally return the temp.

I am such an idiot! I can't believe i didn't see this! Thanks!

> --
> Christopher E. Miller
>


May 03, 2004
"Carlos Santander B." <carlos8294@msn.com> wrote in message news:c766kg$3ad$2@digitaldaemon.com...
> "Ivan Senji" <ivan.senji@public.srce.hr> wrote in message
> news:c756ct$1jsi$1@digitaldaemon.com
> | When happens when delete this is done inside a class?
> | Why doesn't it immediatelly call the destructor?
> |
> | I wrote a class that is ment to be a temporary object
> | return by functions and i would like it to be destructed
> | right after it is used. So i tried:
> |
> | [...]
> |
> | i see that the destructor of ref is called when the program is exited.
> | So what does then "delete this" mean, does it only tell GC that
> | the object can be collected?
>
> You may want to use auto objects or auto classes.

The problem was something else. I can't use auto objects because it isn't possible to return auto created objects.


> -----------------------
> Carlos Santander Bernal
>
>


Top | Discussion index | About this forum | D home