www.digitalmars.com         C & C++   DMDScript  

D - destructor of base class

reply "Pavel Minayev" <evilone omen.ru> writes:
...how do I call it? I've tried ~super() and super.~this(), but
no luck. It seems to dislike the very idea of call to destructor,
in general, for example, I can't even do ~this() - and I could
in C++...
Feb 01 2002
next sibling parent reply Ruslanas Abdrachimovas <anubis 03bar.ktu.lt> writes:
D is GARBAGE COLLECTED... That means: destructors called only from 
garbage collector... No DELETE and no stack objects. =]

Ruslanas

Pavel Minayev wrote:

 ...how do I call it? I've tried ~super() and super.~this(), but
 no luck. It seems to dislike the very idea of call to destructor,
 in general, for example, I can't even do ~this() - and I could
 in C++...
 
 
 

Feb 01 2002
next sibling parent reply "Mike Wynn" <mike.wynn l8night.co.uk> writes:
And I believe D automatically calls your super destructor
if not it should.

This has reminded me of the fun Java VM vendors have with Java's finalise
method.

What does D do if I put a reference to my object into a static of some other
class, or globally visuable Object from my destructor ?
being that the destructor should only be called once (so the docs say) but I
have just make a dieing object live again, very bad I know, but will it get
called again, or will it be destroyed and cause a seggy later.
D does not have Soft or Weak references, I forsee people wanting to revive
objects that are about to be deleted if they've not been live for long
(caching etc).

Mike.

"Ruslanas Abdrachimovas" <anubis 03bar.ktu.lt> wrote in message
news:3C5AB1CA.601 03bar.ktu.lt...
 D is GARBAGE COLLECTED... That means: destructors called only from
 garbage collector... No DELETE and no stack objects. =]

 Ruslanas

 Pavel Minayev wrote:

 ...how do I call it? I've tried ~super() and super.~this(), but
 no luck. It seems to dislike the very idea of call to destructor,
 in general, for example, I can't even do ~this() - and I could
 in C++...


Feb 01 2002
next sibling parent "Pavel Minayev" <evilone omen.ru> writes:
"Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
news:a3eeev$vbg$1 digitaldaemon.com...

 And I believe D automatically calls your super destructor

Nope, I've tested it of course before writing =)
 if not it should.

I see two solutions here: one to insert it automatically at the end of destructor, the other is to let the programmer put it wherever he wants, but the call _must_ happen. This is the same I proposed for constructors.
Feb 01 2002
prev sibling parent Ruslanas Abdrachimovas <anubis 03bar.ktu.lt> writes:
I wrote some examples with construction of two objects of child and base 
classes, there one of them:

....
import object;

class A
{
	private int val;
	
	protected int vA;	

	public this(int v)
	{
		val = v;
		printf("Class A constructor [%d]\n", val);
		init();		
	}
	
	private void init()
	{
		vA = 999;
		printf("Class A init [%d]\n", val);
	}

	public void print(char[] str)
	{
		printf("vA=[%d] str=[%s]\n", vA, (char*)str);
	}

	public ~this()
	{
		printf("Class A destructor [%d]\n", val);
	}
}

class B : A
{
	private int val;	

	public this(int v)
	{
		val = v;
		printf("Class B constructor [%d]\n", val);
		init();
		super(v); // construct base class
	}

	private void init()
	{
		printf("Class B init [%d]\n", val);
	}

	public ~this()
	{		
		printf("Class B destructor [%d]\n", val);
	}
}

int main(char[][] args)
{
	printf("Prog begin\n");

	for (int i = 0; i < args.length; ++i)
	{
		printf("%d = [%s]\n", i, (char*)args[i]);
	}	

	printf("-------------------------------\n");
	A a = new A(0);
	B b = new B(1);
	b.print("Call to super class's method");
	printf("-------------------------------\n");

	printf("Prog end\n");

	return 0;
}
....

We should see two calls to A destructor (for a and for b - if D 
automatically calls super destructor), but if You try, You will see only 
one =) ... D doesn't call superdestructor automatically, but I think, it 
frees memory of super class. ;)

Ruslanas

Mike Wynn wrote:

 And I believe D automatically calls your super destructor
 if not it should.
 
 This has reminded me of the fun Java VM vendors have with Java's finalise
 method.
 
 What does D do if I put a reference to my object into a static of some other
 class, or globally visuable Object from my destructor ?
 being that the destructor should only be called once (so the docs say) but I
 have just make a dieing object live again, very bad I know, but will it get
 called again, or will it be destroyed and cause a seggy later.
 D does not have Soft or Weak references, I forsee people wanting to revive
 objects that are about to be deleted if they've not been live for long
 (caching etc).
 
 Mike.
 
 "Ruslanas Abdrachimovas" <anubis 03bar.ktu.lt> wrote in message
 news:3C5AB1CA.601 03bar.ktu.lt...
 
D is GARBAGE COLLECTED... That means: destructors called only from
garbage collector... No DELETE and no stack objects. =]

Ruslanas

Pavel Minayev wrote:


...how do I call it? I've tried ~super() and super.~this(), but
no luck. It seems to dislike the very idea of call to destructor,
in general, for example, I can't even do ~this() - and I could
in C++...



Feb 01 2002
prev sibling parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Ruslanas Abdrachimovas" <anubis 03bar.ktu.lt> wrote in message
news:3C5AB1CA.601 03bar.ktu.lt...

 D is GARBAGE COLLECTED... That means: destructors called only from
 garbage collector... No DELETE and no stack objects. =]

It doesn't matter. Destructor is something that gets called when object is deleted - whether you use delete or the GC removes it (BTW there _is_ delete in D!). Since destructor is practically just a virtual method, the very idea of OOP demands it to be callable from derived classes. C++ does this, so does Object Pascal... but not D (yet?). And this has nothing to do with GCing.
Feb 01 2002
parent reply "Walter" <walter digitalmars.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:a3egc4$10bm$1 digitaldaemon.com...
 "Ruslanas Abdrachimovas" <anubis 03bar.ktu.lt> wrote in message
 news:3C5AB1CA.601 03bar.ktu.lt...

 D is GARBAGE COLLECTED... That means: destructors called only from
 garbage collector... No DELETE and no stack objects. =]

It doesn't matter. Destructor is something that gets called when object is deleted - whether you use delete or the GC removes it (BTW there _is_ delete in D!). Since destructor is practically just a virtual method, the very idea of OOP demands it to be callable from derived classes. C++ does this, so does Object Pascal... but not D (yet?). And this has nothing to do with GCing.

To explicitly call the destructor, use the delete operator. I'll look into the bug where the super destructor is not happening!
Feb 01 2002
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Walter" <walter digitalmars.com> wrote in message
news:a3er6r$sr$2 digitaldaemon.com...

 To explicitly call the destructor, use the delete operator. I'll look into

Great, so now we can completely control life-cycle of our objects!
 the bug where the super destructor is not happening!

So it is supposed to be called automatically?
Feb 01 2002
parent "Walter" <walter digitalmars.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:a3eru5$vn$1 digitaldaemon.com...
 "Walter" <walter digitalmars.com> wrote in message
 news:a3er6r$sr$2 digitaldaemon.com...

 To explicitly call the destructor, use the delete operator. I'll look


 Great, so now we can completely control life-cycle of our objects!

 the bug where the super destructor is not happening!

So it is supposed to be called automatically?

I don't know yet. <g>
Feb 01 2002
prev sibling parent "Walter" <walter digitalmars.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:a3ea7t$suh$1 digitaldaemon.com...
 ...how do I call it? I've tried ~super() and super.~this(), but
 no luck. It seems to dislike the very idea of call to destructor,
 in general, for example, I can't even do ~this() - and I could
 in C++...

Ok, I've fixed it so the super destructor gets called automatically when the destructor finishes. There's no way to call it explicitly. The obvious question is, why is this not symmetrical with the way super constructors are called (they're called explicitly in the constructor)? The reason is because there can be many different super constructors with different argument lists, and sometimes things must be computed before even passing arguments to the super constructor. Destructors must get called, but there can be only one (!), and so the runtime system calls it automatically.
Feb 04 2002