0

I create objects via new at one place in my program (let's call them actors). Those objects get passed to another object (let's call it a projectile) via their pointer. At any point that actor object can be deleted outside of projectile. delete and setting to NULL happens with this. However a pointer of it is still in projectile. I do check if != NULL but it passes those checks. The values inside the class are all bogus but the pointer itself isn't NULL.

Do I need to do a pointer to a pointer or something? I just thought if I passed a pointer around and at some point deleted and set that pointer to NULL all the other places I passed it would show NULL as well.

2 Answers 2

5

You should probably look into smart pointers, like std::shared_ptr.

Sign up to request clarification or add additional context in comments.

Comments

1

Have you thought about double indirection ?

  1. Use a map of Actors: std::map<unsigned, Actor>
  2. Only ever store the Actor ID in the projectiles

Now, supposing a monotonically increasing ID (you have about 4 billions of them available, should keep you going for a while), an ID being absent from the map means the Actor disappeared (was deleted).

1 Comment

pointer to a pointer is how I'll pass these objects and then check that against NULL. did a small test and it works. thanks for the suggestion though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.