Re: Why people use "new" & "delete" too much?!!
On Jul 5, 10:05 pm, Medvedev <3D.v.Wo...@gmail.com> wrote:
On Jul 5, 11:59 am, red floyd <no.spam.h...@example.com> wrote:
[...]
i know that the object created this way is in the heap
which have much memory than stack but why they always
define objects that way , why not just say "test n" and
the object will be destroyed by itself at the end of the
program! , instead of using "new" and maybe u will forget
to "delete" at the end
Several reasons.
1. They're coming from Java and they don't know any better
2. They're storing polymorphic objects inside containers
3. They need the lifetime of the object to exceed the scope in which
it was declared.
how u can use object after it's scope ends!!
Objects don't have scope, they have lifetime. Scope concerns
the visibility of a declaration (and is linked with the
structure of the program). Lifetime concerns when the object
comes into and goes out of being. C++ defines several different
types of lifetime, some linked to scope (e.g. automatic), and
others not (e.g. dynamic). If you create an object with new, it
has dynamic lifetime, and exists until you delete it. Which
could be somewhere else entirely.
--
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