企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## 概述 <details> <summary> person.h</summary> ``` #ifndef PERSON_H #define PERSON_H #include <QApplication> #include <QObject> #include <QDebug> class Person : public QObject { Q_OBJECT public: explicit Person(QObject* parent=nullptr):QObject(parent){ } static Person* instance(){ static Person* person=nullptr; if(person==nullptr){ qDebug()<<"init person"; // 当处理堆对象时,千万注意不要产生内存泄漏。可以利用 Qobject的父-子关系来防止 person=new Person(qApp); } return person; } void show(){ qDebug()<<__FUNCTION__<<metaObject()->className(); } }; #endif // PERSON_H ``` </details> <br /> 调用 ``` Person::instance()->show(); Person::instance()->show(); // output: // init person // show Person // show Person ```