473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Use of the "new" operator in widget toolkits

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

Jul 23 '05 #1
1 2559
sv******@hotmail.com wrote:
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

Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mysooru | last post by:
How to restrict an object to be instantiated only using new operator? I mean, one should not be allowed to create an object like AnObject obj; it should be AnObject obj = new AnObject;
18
by: Leslaw Bieniasz | last post by:
Cracow, 28.10.2004 Hello, I have a program that intensively allocates and deletes lots of relatively small objects, using "new" operator. The objects themselves are composed of smaller...
24
by: Rv5 | last post by:
Rookie c++ question, but Ive spent the last 5 years doing Java, where everytime I created an object I used new. In c++ I can create my objects without and its confusing me just a little. I have...
7
by: Jamie Julius | last post by:
Consider the following struct: struct TestStruct { public int a, b, c; public TestStruct(int a, int b, int c) { this.a = a;
5
by: Sam | last post by:
Hi everyone Could anyone help me understand the usage of the "New" keyword I'm new to VB.Net. 1. Why do we use the "New" keyword on some object type variables such as the myPen of the...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
7
by: Jim Land | last post by:
In the Yahoo Interface Blog, Douglas Crockford wrote about Javascript: "So the rule is simple: The only time we should use the new operator is to invoke a pseudoclassical Constructor function....
14
by: mlw | last post by:
Do not take anything about this, it is not a flame or troll, while I'm not new to Java I favor C++. However, I may need to use it in a contract position, and am concerned that the restrictions it...
350
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.