Re: Never ever use a raw pointer when a smart pointer can do the same
job
(Never ever use a raw pointer when a smart pointer can do the same
job)
On 13 Aug., 17:15, "Hicham Mouline" <hic...@mouline.org> wrote:
... my colleague says.
I disagree on the "Never".
Opinions about this "rule of thumb" welcome.
regards,
It really depends on what the job of the pointer is. If the pointer
represents a ressource, I tend to agree with your collegue: a delete
should rarely be placed anywhere else but in a class that is
responsible for the ressource itself. Not doing so might allow
ressources to leak in the case of exceptions or whenever the program
path is changed due to the correction of an unrelated error or an
extension of functionality.
Having said that, naked pointers by themselves are not by any means a
sign of bad design. On the contrary, there are loads of places where
they are appropriate. Too many, in fact: a pointer has far too many
purposes: as an optional value, for iteration, for traversal, to
represent polymorphic objects and so on. It really would be nice if
you could specialise elegantly on some of this, and if you can my
recommendation is to use the specialised version or e.g. use boosts
optional instead of raw pointers.
/Peter