[DiscordArchive] When should I delete a pointer?
[DiscordArchive] When should I delete a pointer?
Archived author: Deleted User • Posted: 2022-04-19T17:20:01.094000+00:00
Original source
When should I delete a pointer?
Archived author: Deleted User • Posted: 2022-04-19T17:20:11.164000+00:00
Original source
only when 'new' keyword is used? or every time I declare a pointer?
Archived author: <o> • Posted: 2022-04-19T17:22:10.372000+00:00
Original source
if you don't use the "new" keyword (or call some function that does it for you), you should not delete the pointer. Generally, it's better to use smart pointers and avoid "new" as much as possible
Archived author: Deleted User • Posted: 2022-04-19T17:22:57.808000+00:00
Original source
So, doing something like this:
`std::list<Unit*> targets;`
Would not require to delete?
Archived author: Deleted User • Posted: 2022-04-19T17:23:16.917000+00:00
Original source
oh wait
Archived author: <o> • Posted: 2022-04-19T17:23:18.386000+00:00
Original source
no, because you're not the one responsible for unit pointers. However, be very careful about storing entity references like that
Archived author: Deleted User • Posted: 2022-04-19T17:23:25.464000+00:00
Original source
Yea
Archived author: Deleted User • Posted: 2022-04-19T17:23:36.846000+00:00
Original source
because the Unit pointer will be deleted when Unit gets destroyed
Archived author: Deleted User • Posted: 2022-04-19T17:23:37.873000+00:00
Original source
right?
Archived author: <o> • Posted: 2022-04-19T17:23:50.783000+00:00
Original source
no, it will not, your pointers will be dangling and there is nothing you can do about that