Re: naked pointer vs boost::shared_ptr<T>
On Feb 28, 12:48 pm, "peter koch larsen" <peter.koch.lar...@gmail.com>
wrote:
On Feb 28, 10:24 am, "Sushrut Sardeshmukh" <bestbr...@gmail.com>
wrote:
Should we stop using naked pointer and replace all of them with
boost:shared_ptr or ( shared_array<T> or scoped_ptr) ?
The problem is not so much pointers. My recommendation is that you
should avoid any "delete" or "delete []" in your code.
So how do you delete an object with dynamic lifetime? I agree
with regards to delete[], and I don't think I've used it once in
over 15 years of C++. But delete?
Maybe you meant that all delete should be "delete this"?
delete should be used only in classes whos only purpose is to
maintain the memory resource.
If the only problem is the memory resource, the Boehm collector
does the job a lot better. Some classes have explicit
lifetimes, and the idiomatic way to terminate the lifetime of
such an object is delete.
I am trying to come up with C to C++ conversion guidelines for a large
transformation project. Your inputs will be very valuable.
RAII (of which the above is an example) is THE paradigm to follow.
I'm not sure I follow. RAII obeys scope; if the object lifetime
obeys scope, what are you doing allocating it dynamically to
begin with? There are exceptions, of course, but a lot of the
time, rather than replacing a raw pointer with a smart pointer,
it would be better to do away with the pointer completely.
In my own applications, I find that most pointers are to objects
with explicit lifetimes, and I've yet to find a smart pointer
which is applicable to them (although I've tried). About the
only other cases are polymorphic agents (which are best
collected), and singletons (where dynamical allocation is used
expressedly so that they will never be deleted). In the case of
polymorphic agents, if I can't use the Boehm collector, I will
use either auto_ptr or a shared_ptr of some type.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]