Hi guys
I'm trying to build a memory management class in C++ but I'm stuck in a problem.
One of the features of this module will be automatically deleting "dead" objects, i.e., those that no longer get referenced by any pointers.
Consider the following example:
1 2 3 4 5
|
Dog *d1 = new Dog("Rex");
Dog *d2 = new Dog("Zeus");
Dog *d3 = d1;
d1 = 0;
d2 = 0;
|
We know that "Zeus" should be collected, but "Rex" no. I was wondering a way to figure out that one object is no longer being referenced. The only way I see now is overloading the assignment operator. In order to work with any type of object, I believe it would be necessary to overload the assignment operator with a "void *" type.
My expectation is to get something close to below example.
1 2 3 4 5 6 7 8 9
|
void * operator=(void* left)
{
if (!left)
{
// let's update the list of references and decrease one in the referencing counter
}
// rest of function
return left;
}
|
Is it possible? I have been looking for it with no success. If it's not possible or too difficult to be done, does anyone think of a workaround?
txs