Use of the "new" operator in widget toolkits

S

sven_c_t

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:

-------------
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>

int main(int argc, char **argv) {
Fl_Window *window = new Fl_Window(300,180);
Fl_Box *box = new Fl_Box(20,40,260,100,"Hello, World!");
box->box(FL_UP_BOX);
box->labelsize(36);
box->labelfont(FL_BOLD+FL_ITALIC);
box->labeltype(FL_SHADOW_LABEL);
window->end();
window->show(argc, argv);
return Fl::run();
}
-------------

Both the object of the Fl_Window widget and the Fl_Box widget, is
created using the new operator. I dont see the point in using dynamic
memory allocation when it is not stricly nessesary. What is the
advantage in this situation?

Likewise Qt makes use of the new operator when dealing with widgets.
The following example is taken from an Qt tutorial:

-------------
#include <QApplication>
#include <QFont>
#include <QPushButton>

class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
};

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
setFixedSize(200, 120);

QPushButton *quit = new QPushButton("Quit", this);
quit->setGeometry(62, 40, 75, 30);
quit->setFont(QFont("Times", 18, QFont::Bold));

connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}
--------------

Again, I dont see the necessity for dynamic memory allocation, even
though it is not nessesary within this framework to think about
deleting the child widgets.

I hope that someone has an answer for me, because this question is
simply killing me...


Sven Creutz Thomsen
 
J

Jonathan Mcdougall

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:

<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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top