Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am writing a program that requires me to keep track of how many copies of a certain object are in creation at any given time. There are several ways that I have thought of doing this:
1) This is the most simple. I create a static int which is incremented each time an object is created in its constructor. Then, I provide a function to be called just before the object goes out of scope. This function would decrement the counter. However, this approach would require the client to manually call this function, and could lead to erroneous counting.
2) I was wondering if there is a way to include a "destructor" in a object, similar to C++. I have not heard of one, but it would be nice if there is one.
3) If there are any other ways to accomplish this, please do tell.
Thanx.

No, finalize() is the closest thing. There is a reference API. You might consider manipulating the reference queue.

Are you using threading? If so, activeCount() will return the number of threads. Writing your own method may work ok, but will probably be inaccurate anyways.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |