sven_c_t@hotmail.com wrote:[color=blue]
> Hi!
>
> Probably a newbie question. I´ve been working a bit with the widget
> toolkit FLTK and I have been wondering why there is such a heavy use of
> the new operator, when it does not seem to be nessesary (at least to
> me). What follows is an example taken from the FLTK tutorial:[/color]
<snip>
There are two fundamental restrictions with GUIs:
1) objects must live as long as their parent (button as long as the
window, for example)
2) the identity of each object (widget) is important
To solve 1), you must make copies of the objects. For example, a Window
could hold a vector of Buttons by value. These will live as long as the
Window is alive.
The problem is that it violates 2). When you make a copy of a button,
you get two buttons. Modifying the original will not change what you
see on your screen because the Window has a copy of the button. You may
ask the Window for its copy to be able to modify it: that would solve
the problem. But it is frequent in user interfaces to see several
controls referring the same one. For example, a TextBox might add a *
on the title bar of the Window to signal to the user that something has
changed and the document must be saved.
So usually, you'll end up using pointers to controls. Since you need
the objects to live long enough, you'll have to allocate them on the
heap, with new. You usually don't need to delete them because the
containers (such as windows or panels) will delete their children
automatically. You will typically need to delete only your top-level
windows.
Some systems hide the pointers behind a handle, such as Win32's HWND.
There is a table somewhere which links a HWND (a handle) to a pointer
to a control.
Jonathan