The app variable is created on the stack in the QApplication app(argc, argv); line QApplication app(argc, argv); , it invokes the QApplication constructor with arguments argc and argv , which in this case creates a QApplication object called app .
This is not a feature of Qt. You can also select any non-virtual class using the constructor. In addition, it works with primitives, so you can do this, for example:
int val(1);
which would create an integer variable named val with a value of 1.
You can allocate a QApplication object on the heap with new and use it as follows:
QApplication* app = new QApplication(argc, argv); // Heap allocation app->setOrganizationName("My Company"); // Notice the -> instead of . app->setApplicationName("The App");
-> is basically a shortcut for dereferencing a pointer and using it .
Allocation on the stack is usually preferable (but not always possible), because then you do not need to worry about the lifetime of the object (using any smart pointer or raw delete ), stack allocation is also usually less expensive than allocating a heap.
source share