在主事件循环之前如何创建一些对象?

How some objects are created before main event loop?

在调试时,我想出了一个奇怪的事情。在main函数中,我注释掉了window的创建如下:

#include <QApplication>
#include <QMetaType>
#include <QDebug>

//#include "mainwindow.h"

int main(int argc, char *argv[])
{
    qDebug() << "Creating QApplication";
    QApplication a(argc, argv);
    qDebug() << "QAplication has been created successfully";

    qDebug() << "Creating application window";
    // MainWindow w;
    qDebug() << "Displaying application window";
    // w.show();
    qDebug() << "Application window has been displayed successfully";

    return a.exec();
}

我以为我只是在创建一个事件循环并使用它。但输出让我感到惊讶:

"17:28:32.793" ButtonControl: contructor.
"17:28:32.807" GestureControl: contructor
Creating QApplication
QAplication has been created successfully
Creating application window
Displaying application window
Application window has been displayed successfully

我有 ButtonControlGestureControl classes,前两行输出来自它们的构造函数。我在 other class 中创建它们的对象,我在 MainWindow class 中使用它们。对我来说奇怪的是它们被创建 before/without MainWindow 和事件循环。即使我不创建 QApplication 对象并调用它的 exec() 方法,它们的消息也会被打印出来。我尝试了 cleaning 和 运行ning qmakerebuild ing,但没有成功。这里发生了什么? ButtonControlGestureControl classes 有什么问题?

环境:win7、Qt 5.10、MSVC 2015。


编辑

这是我的 Control class:

class Control : public QObject
{
    Q_OBJECT
public:
    explicit Control(QObject *parent = nullptr);
    static ButtonControl *getButtonController() {
        return &buttonController;
    }
    static GestureControl *getGestureController() {
        return &gestureController;
    }

private:
    static ButtonControl buttonController;
    static GestureControl gestureController;
};

我在主窗口中调用此 class。 (正如我从评论中了解到的,这个代码片段就足够了)

根据评论,我做了一个小调查,找到了 this 答案:

Static members are initialized before main(), and they are destroyed in reverse order of creation after the return in main().

Static members are statically allocated, and their lifetime begins and ends with the program.

谢谢大家的评论。