[Qt-interest] global variables new and delete

Malyushytsky, Alex alex at wai.com
Thu Sep 10 22:41:38 CEST 2009


In other words, don't create any widgets before QApplication instance is created.
Side note. Global objects are evil.
Avoid them.

If you need something persistent during application lifetime,
override QApplication class, add appropriate objects as members.
And you will have full control over their lifetime.
More of that if such object class is derived from QObject, give it a parent (qApp for example),
it will be deleted when QApplication object is destroyed.

Regards,
   Alex

-----Original Message-----
From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of John McClurkin
Sent: Thursday, September 10, 2009 9:09 AM
To: qt-interest at trolltech.com
Subject: Re: [Qt-interest] global variables new and delete

Serge wrote:
> Hello,
> please help me to understand how to do it correctly:
>
> i have file globals.h with lines:
> class MyClass;
> namespace globals
> {
>     extern MyClass *myPtr;
> }
>
> in globals.cpp:
> MyClass *globals::myPtr=NULL;
>
> where can i create  by "new" this object and delete by "delete"?
> so that it would be created before all widgets of my app created and
> destroyed on application close after all widgets destroyed?
>
> main.cpp contains lines:
>
> QApplication a( argc, argv );
globals::myPtr = new MyClass();

> return a.exec();
>
delete globals::myPtr;
return 0;
> --
> Serge
_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest


---------------------------------------------------------------------------------------------------
Weidlinger Associates, Inc. made the following annotations.

"This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you."

"Please consider our environment before printing this email."




More information about the Qt-interest-old mailing list