0

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:

  1. 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?)

  2. 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.

  3. Is there some cooler/better way to do what I want to do?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user487100
  • 918
  • 1
  • 11
  • 22
  • 3
    It looks like what you require is a smart pointer. C++ has `std::shared_ptr` for this purpose. – Asha Feb 23 '12 at 04:45
  • can I use a smart pointer anywhere I would use a regular pointer? Because I will need to use a method like someFrameworkClass::setUserInfo( VirtualUserInfo* myUserInfo); – user487100 Feb 23 '12 at 04:48
  • Yes, but note that there are different types of smart pointers. Take a look here for details: http://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i-use-one For getting a raw pointer out of smart pointer you have to use methods like `get`. – Asha Feb 23 '12 at 04:50
  • 1
    No, you can't pass a smart pointer directly to the framework which expects a raw pointer. However, you can put a `shared_ptr` inside your `UserInfo`, which points to the real data structure. – Ben Voigt Feb 23 '12 at 04:53
  • Thanks, that's much easier than what I was going to try to do. You should add that as an answer! – user487100 Feb 23 '12 at 04:57
  • Thinking about it, though, the drawback is I still need to instantiate a wrapper class instance around an instance of my smart pointer for every particle. Since creating millions of objects was what I was hoping to avoid, I'd still be interested in answers to my original question. – user487100 Feb 23 '12 at 08:59

2 Answers2

0

I'm not sure why you would need a wrapper class...can you really not change your Particle class interface? The pointer-to-userinfo part would just look like this:

class Particle {
   std::shared_ptr<VirtualUserInfo> mpUserInfo;
public:
   Particle(std::shared_ptr<VirtualUserInfo> UserInfo):
      mpUserInfo(UserInfo)
   {}
};

Anything you try to implement yourself will likely just be duplicating what is already available with std::shared_ptr, and this may end up being more work than changing your interface.

Carlton
  • 4,217
  • 2
  • 24
  • 40
0

Why do you need the crtp?? Use could use the shared_ptr in a new standard or boost library, also you could implement your own reference counted smart pointer if your compiler does not support boost or a new standard, just take a look here: http://code.google.com/p/tnnlib/source/browse/trunk/%20tnnlib/Utilities/Utilities/SmartPtr/SmartPtr.h

AlexTheo
  • 4,004
  • 1
  • 21
  • 35