By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,925 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,925 IT Pros & Developers. It's quick & easy.

Use of the "new" operator in widget toolkits

P: n/a
Hi!

Probably a newbie question. Ive 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

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
sv******@hotmail.com wrote:
Hi!

Probably a newbie question. Ive 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

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.