none said:
Hello,
At work we have someone who believes that using new&delete is a hideous
sin. He believes it should never be used. Instead scoped_ptr is the
enlightened way.
Does anyone have a view?
I think you confuse some things. Even when using scoped_ptr one has to use
new to get the memory in the first place (until C++0x with rvalue
references will provide for a way to have "perfect forwarding" functions
and then you can call "scoped_ptr_new<Class>(parameters)" instead of
the "new" expression).
I think what the person tells you is that you should not play with "blind
pointers" (as I call them) but instead always wrap them into something that
expresses the ownership semantics you wish to have over that allocated
object. For that you may use scoped_ptr, auto_ptr, shared_ptr and whatever
pointer wrapper you make yourself.
Instead of:
Class* ptr = new Class;
Do:
scoped_ptr<Class> ptr(new Class);
It provides for exception safe, less error prone code (you dont need to
worry about deleting anymore unless you cache a pointer/reference to that
object in code outside of that scope and you haven't released the ownership
of scoped_ptr).
See
http://en.wikipedia.org/wiki/RAII or google about it (other people
prefer the term "Scope Bound Resource Management" considering RAII a bad
term for what it does).