20.3.1.  Globals, static, and QObject

[ fromfile: globals.xml id: globals ]

There are two reasons generally used to justify giving an object global scope.

  1. It needs a lifetime that is the same as the application.

  2. It needs to be accessible from several different places in the application.

In C++, you should avoid the use of global scope as much as possible and instead use other mechanisms. However, you can still use global scope identifiers for the following:

  1. Class names

  2. Namespace names

  3. The global pointer qApp, which points to the running QApplication object.

By turning a global object into a static class member, or a namespace member, you can avoid increasing the size of the global namespace while keeping the object accessible to several source code modules.



[114] By "interesting" we mean any class with a destructor that has some important cleaning up to do.