现在的位置: 首页 > 综合 > 正文

由Qt中qApp想到的(这是单例模式么???)

2018年04月02日 ⁄ 综合 ⁄ 共 2522字 ⁄ 字号 评论关闭

From: http://blog.csdn.net/qq575787460/article/details/7880972

学Qt时,发现只要包含头文件QApplication或者QCoreapplication,就用使用qApp,指向一个当前实例。

查看源码发现qApp是个宏:

QApplication中:#define qApp (static_cast<QApplication *>(QCoreApplication::instance()))

QCoreapplication中:#define qApp QCoreApplication::instance()

所以可以肯定的是QCoreApplication有个静态函数instance()返回指向自己的指针。

  1. static QCoreApplication *instance() { return self; }  

这个函数返回self,看大片self也定义为静态指针:

  1. static QCoreApplication *self;  

在QCoreapplication.cpp中有如下代码:

  1. void QCoreApplication::init()  
  2. {  
  3.     ......  
  4.     QCoreApplication::self = this;  
  5.     ......  
  6. }  

QCoreapplication构造函数中又调用init()

  1. QCoreApplication::QCoreApplication(QCoreApplicationPrivate &p)  
  2.     : QObject(p, 0)  
  3. {  
  4.     init();  
  5.     // note: it is the subclasses' job to call  
  6.     // QCoreApplicationPrivate::eventDispatcher->startingUp();  
  7. }  

所以就是:定义程序唯一的实例时,初始化self指向自身。qApp是一个宏,替换后为调用相应的instance()。

自己模仿的:

myApplication.h

  1. #ifndef MYAPPLICATION_H  
  2. #define MYAPPLICATION_H  
  3.   
  4. #include <string>  
  5. using std::string;  
  6.   
  7. #define myApp myApplication::instance()  
  8.   
  9. class myApplication  
  10. {  
  11. public:  
  12.     myApplication(string _appName);  
  13.   
  14.     static myApplication* instance(){return self;}  
  15.     void printAppName();  
  16. private:  
  17.     //指向自己的实例句柄  
  18.     static myApplication *self;  
  19.     string appName;  
  20. };  
  21.   
  22. #endif // MYAPPLICATION_H  

myApplication.cpp

  1. #include "myapplication.h"  
  2. #include <iostream>  
  3. using std::cout;  
  4. using std::endl;  
  5.   
  6. myApplication::myApplication(string _appName):appName(_appName)  
  7. {  
  8.     if(self!=0)  
  9.         return;  
  10.     else  
  11.         self=this;  
  12. }  
  13.   
  14. void myApplication::printAppName()  
  15. {  
  16.         cout<<appName<<endl;  
  17. }  
  18.   
  19. myApplication* myApplication::self=0;  


main.cpp

  1. #include "myapplication.h"  
  2.   
  3. extern void print1();  
  4. extern void print2();  
  5.   
  6. int main()  
  7. {  
  8.     print1();  
  9.     print2();  
  10.   
  11.     myApplication app("实例测试");  
  12.     print1();  
  13.     print2();  
  14.     app.printAppName();  
  15.     myApp->printAppName();  
  16.   
  17.     return 0;  
  18. }  

print1.cpp

  1. /* 
  2.   测试函数,测试通过myApp输出唯一的实例名 
  3. */  
  4. #include "myapplication.h"  
  5. #include <iostream>  
  6.   
  7. void print1()  
  8. {  
  9.     if(myApp==0)  
  10.         std::cout<<"myApp没有实例化"<<std::endl;  
  11.     else  
  12.         myApp->printAppName();  
  13. }  

print2.cpp

  1. /* 
  2.   测试函数,测试通过myApp输出唯一的实例名 
  3. */  
  4. #include "myapplication.h"  
  5. #include <iostream>  
  6.   
  7. void print2()  
  8. {  
  9.     if(myApp==0)  
  10.         std::cout<<"myApp没有实例化"<<std::endl;  
  11.     else  
  12.         myApp->printAppName();  
  13. }  

抱歉!评论已关闭.