Overload assignment operator with void *

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
Is it possible?


No. Operator overloading can only be done with custom types (ie: classes/structs).

Also... FWIW, this problem is a nonissue if you use smart pointers. If you are manually deleting your objects, you are doing it wrong.
txs
If someone has the same doubt I had, I'm pasting this code I made myself as an example. C++ has its own smart pointers classes, I made this one as an excercice. I came from Java so memory is something I'm always worried about in C++.

You can easily run this code at http://www.compileonline.com/compile_cpp11_online.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <new>

using namespace std;

template <typename T>
class SmartWrapper
{
    private:
    T * obj;
    public:
    SmartWrapper(T * newObj)
    {
        cout << "Wrapper: Begin" << endl;
        obj = newObj;
    }
    ~SmartWrapper()
    {
        delete obj;
        cout << "Wrapper: End" << endl;
    }
};

class Person
{
    public:
    Person()
    {
        cout << "Person: Begin" << endl;
    }
    ~Person()
    {
        cout << "Person: End" << endl;
    }
};

class Dog
{
    public:
    Dog()
    {
        cout << "Dog: Begin" << endl;
    }
    ~Dog()
    {
        cout << "Dog: End" << endl;
    }
};

int main()
{
    // we should have the same number of begins and ends.
    // we actually have half of ends because objects created
    // without smart pointers are not deleted after each iteration.
    for(int i = 0; i < 2; i++)
    {
        Dog* c1 = new Dog();
        Person* c2 = new Person();
        SmartWrapper<Dog> s1(new Dog());
        SmartWrapper<Person> s2(new Person());
    }
   
   return 0;
}



Dog: Begin
Person: Begin
Dog: Begin
Wrapper: Begin
Person: Begin
Wrapper: Begin
Person: End
Wrapper: End
Dog: End
Wrapper: End
Dog: Begin
Person: Begin
Dog: Begin
Wrapper: Begin
Person: Begin
Wrapper: Begin
Person: End
Wrapper: End
Dog: End
Wrapper: End
We typically use http://www.ideone.com/ or http://coliru.stacked-crooked.com/ for online compilation because you can share links to the result of compilation.

http://ideone.com/mZvlKI
http://coliru.stacked-crooked.com/a/70417c228fee237b

In case you didn't already know, what you're taking advantage of here is RAII ;)
> Dog* c1 = new Dog();
just in case you don't know, you can create object by doing simply Dog c1;
Topic archived. No new replies allowed.