digitalmars.D - question about delete.
- Ivan Senji (47/47) May 03 2004 When happens when delete this is done inside a class?
- Ivan Senji (4/51) May 03 2004 Sorry first sentence should have been:
- vathixSpamFix dprogramming.com (Vathix) (8/67) May 03 2004 The return statement causes execution to immediately jump back to the
- Ivan Senji (4/73) May 03 2004 use
-
Carlos Santander B.
(17/17)
May 03 2004
"Ivan Senji"
wrote in message - Ivan Senji (4/21) May 03 2004 The problem was something else. I can't use auto objects
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...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. MillerWhen 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
"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...useSorry 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...The return statement causes execution to immediately jump back to the caller; the delete statements are not reached in your code. You need toWhen 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?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
"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
"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
May 03 2004