I rely somewhat heavily on destructors in my programming. So the question I have here is how I can simulate this in garbage collection languages like C#. I program primarily in C++, but am looking for things I can do to make an eventual port to C# easier.
For instance, say I have a skeleton class that always keeps a variable updated somewhere...
class Skeleton
{
Skeleton() {gSkeletonCount++;}
~Skeleton() {gSkeletonCount--;}
};
Now, each "new Skeleton" increases gSkeletonCount, and each "delete Skeleton" decreases gSkeletonCount, and I always know how many of my skeletons are alive.
To convert this to C#-ish (or Java) compatibility, my initial inclination is to do this:
class Skeleton
{
Skeleton() {gSkeletonCount++;}
Destructor() {gSkeletonCount--;}
};
#ifdef NOAUTOGARBAGECOLLECTION
#define Delete(Object) {Object->Destructor();delete Object;}
#else
#define Delete(Object) {Object->Destructor();}
#endif
Simple enough!
BUT, and here's my question...
How could I make this compatible with other aspects of my code? For instance, I can't always count on having Destructor() defined... so if I'm going to say new int and delete(int), I will have to have a version of my defined "delete" that does NOT call Destructor(). That sort of sucks... are there any high-end preprocessor operations that could get around that?
And, I'm not even sure how I could wrangle this:
Skeleton *aSkellies=new Skeleton[25];
delete [] aSkellies;
...how could I make a define for delete [] that would actually call the Destructor() on each item?
Thanks for any help you can give!