I am working with a simulation framework. Each particle that is generated has a slot for a pointer to a UserInfo object (so you can attach whatever info you need to the particle). The problem is that the framework deletes this user information whenever the particle gets killed. Since there are many millions of particles, often with duplicate information, I only want to create a new UserInfo object when the information is different. The problem of course is that whenever a particle gets killed, it will delete the UserInfo object it has a pointer to (regardless of whether that same object is also attached to some other particle.)
What steps do I need to take to keep the Particle from deleting the UserInfo object when the particle is killed? I realize I need to do some reference counting and overload delete for my UserInfo class. However, I have never overloaded delete before so I have a few questions:
If I have a class hierarchy like:
class VirtualUserInfo; class A : public VirtualUserInfo; class B : public A ...etc
and I overload delete in class A, will it work if delete is called on a pointer to VirtualUserInfo or on a pointer to class B? (Very similarly, how can you overload new to work correctly? Do you need to overload new again for each new derived class?)
It is easy to do reference counting with the curiously recurring template pattern. Is there a way to also include the delete behavior in such a mixin-style fashion? It would be nice to apply this type of behavior to any type of UserInfo I were to write.
Is there some cooler/better way to do what I want to do?