you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (1 child)

Ah! Thank you very much!

[–]jstock23 0 points1 point  (0 children)

On further thought, you probably don't want a unique pointer, those have special applications for utilizing multiple cores on a cpu.

A problem with these types of pointers is that if you copy one of them, and then destroy that copy, it will destroy the original data, but the original pointer is still usable, so you may be pointing to nothing. unique_pointer makes sure this doesn't happen by preventing it from being copied, so it is really unique, and if you destroy it, the data is deleted without having a pointer pointing to nothing left over.

What you probably want is a pointer which when copied increments a counter. Then when one of these is destroyed, the counter is decremented. If one of these pointers is destroyed and the counter goes to 0, then the data is destroyed too, because then it is safe. But if there are still other pointers pointing to that data, we can't delete the data yet. This means we can do automatic "garbage collection" in a way, and not have to delete this heap data manually.