473,472 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to implement a destructor?

Hi,

I'm looking for a way to implement a destructor as a member method of a
class.

/* BEGIN CODE */

function SomeClass() {

this.constructor.numInstances++;

this.destroy = function () {

this.constructor.numInstances--;
/*
Need someway here to delete the instance
*/

};
}

SomeClass.numInstances = 0;

sc = new SomeClass();

// SomeClass.numInstances = 1

sc.destroy();

// SomeClass.numInstances = 0 and sc is not referenced anymore

/* END CODE */

If there was only a delete operation involved (as in -- delete sc -- in
the above exemple) , I wouldn't need a destructor, but since there is
one operation more (decrementing the number of instances; and there
could be more), and I do not want to delegate the full destruction
process to the procedure which instantiated the object, I need to
implement a destructor as a member method.

Any help would be appreciated on this.

F.

Apr 8 '06 #1
11 52815
François wrote:
Hi,

I'm looking for a way to implement a destructor as a member method of a
class.

Why? delete in JavaScript isn't the same as in Java or C++.

--
Ian Collins.
Apr 9 '06 #2
Ian,

I need to implement it so I can delete an instance and provide at the
same time a chain of actions triggered by the destruction:

sc.destroy() --> deletes sc and --> decrements the number of instances
and --> do some other action (triggers an event, pops up an alert("The
object sc has been destroyed") or some other actions, all part of the
destruction process)

I could do all this outside the class, in the calling procedure:

sc = new SomeClass();
delete sc;
SomeClass.constructor.numInstances--;
event = new CustomEvent();
alert("The object sc has been destroyed");

It seems to me that it would break encapsulation: the SomeClass class
bleeds outsides its boundaries.

Thanks for the reply

F.

Apr 9 '06 #3
Ian,

I need to implement it so I can delete an instance and provide at the
same time a chain of actions triggered by the destruction:

sc.destroy() --> deletes sc and --> decrements the number of instances
and --> do some other action (triggers an event, pops up an alert("The
object sc has been destroyed") or some other actions, all part of the
destruction process)

I could do all this outside the class, in the calling procedure:

sc = new SomeClass();
delete sc;
SomeClass.constructor.numInstances--;
event = new CustomEvent();
alert("The object sc has been destroyed");

It seems to me that it would break encapsulation: the SomeClass class
bleeds outsides its boundaries.

Thanks for the reply

F.

Apr 9 '06 #4
François wrote:
Ian,
You will have more success on Usenet if you learn and follow the normal
Usenet posting conventions. See:-

<URL: http://www.jibbering.com/faq/ >
I need to implement it so I can delete an instance and provide
at the same time a chain of actions triggered by the destruction:

sc.destroy() --> deletes sc

<snip>

Calling a method of an object that cleans up (in some sense) can work
effectively on the internal details of the object but you are not going
to have much joy trying to act upon what in practice is an arbitrary
Identifier referring to the object in some arbitrary scope.

You don't really need to delete the Identifier (and if you declared it
you probably cannot delete it anyway as declared variables are marked
'DontDelete' when they are created). It would be sufficient in practice
to assign the Undefined or Null values to those Identifiers (which
should free the objects referred to for garbage collection).

I suspect that insisting that your destructor is called as:-

sc = sc.destroy();

- and having the method return null explicitly or return nothing
(effectively the Undefined value) is the best you can achieve as far as
acting upon the Identifier is concerned.

Richard.
Apr 9 '06 #5
Thank you: simple and neat solution, that does exactly what I need.

About posting conventions: where did I stray from them? I do indeed
want to respect the list rules.

Regards,

F.

Apr 9 '06 #6
François wrote:
Thank you: simple and neat solution, that does exactly what I need.

About posting conventions: where did I stray from them? I do indeed
want to respect the list rules.

Have a look at <http://cfaj.freeshell.org/google/>.

--
Ian Collins.
Apr 9 '06 #7
Got it: thanks.

F.

Ian Collins wrote:
Have a look at <http://cfaj.freeshell.org/google/>.

--
Ian Collins.


Apr 9 '06 #8
François wrote:
<snip>
About posting conventions: where did I stray from them?
I do indeed want to respect the list rules.


You failed to appropriately quote the message that you are responding
to. See the FAQ:-

<URL: http://www.jibbering.com/faq/ >

- and the more detailed explanation of posting style that it links to.
Then, if the explanation provided is not sufficiently clear, ask for
clarification. (And don't let yourself be fooled into thinking that the
way in which you are viewing this newsgroup has any baring on how anyone
else may be viewing it.)

Richard.
Apr 9 '06 #9
François wrote:
Got it: thanks. <snip>

I am afraid that you have not yet 'got it'. You have gone from not
quoting the message you are responding to, to top-posting and failing to
appropriately trim the material that you are quoting.
Ian Collins wrote:

<snip>

Richard.
Apr 9 '06 #10
JRS: In article <11**********************@i40g2000cwc.googlegroups .com>
, dated Sat, 8 Apr 2006 21:38:25 remote, seen in
news:comp.lang.javascript, =?iso-8859-1?B?RnJhbudvaXM=?=
<fr*************@gmail.com> posted :
Thank you: simple and neat solution, that does exactly what I need.

About posting conventions: where did I stray from them? I do indeed
want to respect the list rules.


If you had been told what you did wrong, that would, hopefully,
eliminate one class of error from your future posts.

You were told where to read about conventions, which *should* eliminate
*all* of the well-known errors. See also via sig line 2 below.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME ©
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Apr 9 '06 #11

"Dr John Stockton" <jr*@merlyn.demon.co.uk> kirjoitti viestissä
news:2s**************@merlyn.demon.co.uk...

....
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME © Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A. Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

"about usage of News.No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail

News"

Why not to add:

"Be as harsh, unfriendly, aggressive as those wise oldtimers, who must(?)
answer dumb questions constantly.'
Apr 10 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
3
by: Brett Hall | last post by:
I have a VB.NET interface that my managed C++ code is to implement. I seem to be stuck implementing an event defined in that interface. Does anyone have a simple code snippet that will show me...
35
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a...
11
by: AB | last post by:
Hi All, I've got an array of objects, during the execution of the program I'd like to assign a particular object to a certain element in the object array. The sample code's like this... class...
23
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624....
7
by: sam | last post by:
Hi, See when i reading a sourcecode of a program, I read that the constructor is ordinary and after that the programmer has written virtual destructor for that constructor . Why we use the...
4
by: Zytan | last post by:
I know two possible ways: 1. Use AppDomain.DomainUnload Event http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemAppDomainClassDomainUnloadTopic.asp which is kinda like Turbo Pascal,...
2
by: ecestd | last post by:
how do you implement a copy constructor for this pointer-based ADT queue #include "Queuep.h" #include <cassert> #include <new> using namespace std; Queue::Queue () : backPtr (0), frontPtr(0)...
6
by: Pallav singh | last post by:
Hi All How Does compiler implement virtual destructor ??? as we all know if base class destructor is virtual..........then while wriiting statemnt like this Base * b = new Derived( );...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.