Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: Why use new...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 18 of 23 Topic 44133 of 48417
Post > Topic >>

Re: Why use new?

by James Kanze <james.kanze@[EMAIL PROTECTED] > Mar 28, 2008 at 01:31 AM

On Mar 27, 8:37 pm, Paul Brettschneider <paul.brettschnei...@[EMAIL PROTECTED]
>
wrote:
> James Kanze wrote:
> > Paul Brettschneider wrote:
> >> James Kanze wrote:

> >     [...]
> >> >> But when are there any reason to use new?

> >> > Lots.  Most of the time, you use new because you need an
> >> > explicit lifetime for the object.  You create it in some low
> >> > level function, in response to an external event, and you delete
> >> > it in some other low level function, in response to some other
> >> > external event.

> >> I guess with the advent of containers with non-copyable
> >> elements, more and more of those new() calls will be hidden in
> >> std::container::insert() calls and the like.

> > I'm not sure what containers you're talking about, but I very
> > much doubt that any of the calls to new would be found in their
> > insert() functions.

> > In the scenario I described (a very, very frequent one for
> > servers, GUI's and probably a number of other applications), the
> > new is in response to an external event, as is the delete.
> > Containers don't repond to external events.

> My idea was that you will have code like this (totally made up
> this moment):

> Handle open_window(const std::string &title)
> {
>    extern std::map<Handle, Window> Windows;
>    Handle h(Windows);      // Generates unique handle
>    Window &w =3D Windows[h];

>    w.set_title(title)
>    return h;
> }

Which, of course, typically won't work, because cl***** like
Window typically don't sup****t copy.  Also, they are
polymorphic, so the actual type will be something derived from
Window.  And of course, the standard containers don't use the
new operator either, since they separate allocation and
initialization.

Generally speaking: if an object has an explicit lifetime, it
almost always also has identity, and so won't sup****t copy, and
can't be put in a standard container.  And the standard
containers don't sup****t polymorphism either.  There are cases
where it might be appropriate to have a container own an object,
i.e. by containing it via some sort of smart pointer, but they
are fairly rare.  Most of the time, the container is at the
service of some higher level concept, and it is the
implementation of the higher level concept (often the object
itself) which takes charge of managing object lifetime.

> Here the new() is hidden in operator[] of std::map.  IMHO new
> and delete are low-level functions (or call them operators)
> and application developers should not have to care too much
> about them. But maybe I'm just silly?

Maybe.  In the applications I work on, or have worked on (large
scale servers, mostly, but also one GUI framework), the
application had to deal with arbitrary lifetimes of entity
objects, and with polymorphic objects, whereas the lower levels
mainly dealt with value objects, or just shuffled pointers
around, and used templates more than dynamic allocation.  If the
lifetime of an object is part of the application design, then
the application programmers will have to deal with it.  And if
the choice of which polymorphic type to use is determined by
the application, application programmers will have to deal with
it.  In my experience, the higher you go in the abstraction
levels, the more the new operator becomes relevant.  (There are
some low level structures where it would be relevant as well,
things like graphs and such.  I've not had to deal with them
much myself, however.)

--
James Kanze (GABI Software)             email:james.kanze@[EMAIL PROTECTED]
 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
 




 23 Posts in Topic:
Why use new?
saneman <asdfsdf@[EMAI  2008-03-26 19:04:24 
Re: Why use new?
"Victor Bazarov"  2008-03-26 14:35:32 
Re: Why use new?
saneman <asdfsdf@[EMAI  2008-03-26 20:32:51 
Re: Why use new?
"Victor Bazarov"  2008-03-26 15:41:53 
Re: Why use new?
James Kanze <james.kan  2008-03-26 15:12:39 
Re: Why use new?
sean_in_raleigh@[EMAIL PR  2008-03-26 11:36:13 
Re: Why use new?
"jason.cipriani@[EMA  2008-03-26 12:33:03 
Re: Why use new?
James Kanze <james.kan  2008-03-26 15:10:05 
Re: Why use new?
Paul Brettschneider <p  2008-03-26 23:43:30 
Re: Why use new?
peter koch <peter.koch  2008-03-26 17:38:33 
Re: Why use new?
"Fred Zwarts" &  2008-03-27 09:47:21 
Re: Why use new?
James Kanze <james.kan  2008-03-27 02:45:58 
Re: Why use new?
Paul Brettschneider <p  2008-03-27 20:37:12 
Re: Why use new?
Ian Collins <ian-news@  2008-03-28 09:10:15 
Re: Why use new?
Paul Brettschneider <p  2008-03-27 21:59:20 
Re: Why use new?
Ian Collins <ian-news@  2008-03-28 10:15:11 
Re: Why use new?
James Kanze <james.kan  2008-03-27 02:50:30 
Re: Why use new?
James Kanze <james.kan  2008-03-28 01:31:59 
Re: Why use new?
Paul Brettschneider <p  2008-03-28 21:39:23 
Re: Why use new?
James Kanze <james.kan  2008-03-28 01:44:16 
Re: Why use new?
Ian Collins <ian-news@  2008-03-28 22:48:35 
Re: Why use new?
James Kanze <james.kan  2008-03-28 15:27:00 
Re: Why use new?
Andrey Tarasevich <and  2008-03-27 00:05:14 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Nov 21 9:36:14 CST 2008.