Re: destructing error
On Feb 2, 4:42 am, Grizlyk <grizl...@yandex.ru> wrote:
James Kanze wrote:
michael.gooss...@gmail.com wrote:
Vector *v2 = new Vector();
Vector *v3 = new Vector();
...
delete (*v2);
delete (*v3);
It is highly recommened to avoid naked pointers at least
outside of class scope,
Bullshit. Most of the time, pointers outside of class scope are
only used for navigation, and naked pointers are the most
appropriate tool.
It is evidently, in the example we are speaking about naked pointers
used to hold value returned from "new", used as owners of memory,
rather than the pointers used to access.
C-style pointers sholud not be used as owners. If the pointer is a
member of a class, the class became owner, so we can use the pointer
to hold value from "new".
In this example, yes. Except that in this example, the error
isn't using raw pointers instead of smart pointers, the error is
using pointers to begin with, rather than local objects. The
recommendation: "It is highly recommened to avoid naked pointers
at least outside of class scope" is bullshit precisely because
there are very few cases where you should use a pointer outside
of class scope other than for navigation. In most cases (there
are exceptions), you're dealing with either entity objects (and
so only navigating) or you're dealing with values (in which
case, you shouldn't be using pointers at all).
Of course, in the example code, you really don't want pointers
at all.
The Vector does not look like std::vector, so the Vector could be
allocated by new here.
If the name is Vector, one can, I assume, that it has container
semantics. At any rate, it is a single object whose lifetime
corresponds exactly to that of a local object. In other words,
he should be using a local variable, and not allocating on the
heap at all.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34