通过singleton模式和global static variable变量做C++程序各个模块之间的数据通信
来源:互联网 发布:数据分析招聘 泉州 编辑:程序博客网 时间:2024/05/21 06:24
提示:这个办法可以用在Qt C++程序和Qt QML的数据交换
首次初始化:DDocumentSets::getInstance()->setModel(oa_model);
以后任何class中都可以获得这个实例: DDocumentSet* oa_model = DDocumentSets::getInstance()->getModel();
并调用其方法: oa_model->xxxx();
header文件:
#ifndef DDOCUMENTSETS_H_#define DDOCUMENTSETS_H_#include "DDocumentSet.hpp"class DDocumentSets {private:static DDocumentSets* _instance;DDocumentSets();DDocumentSet* model;public: static DDocumentSets* getInstance(); void setModel(DDocumentSet* oa_model); DDocumentSet* getModel();virtual ~DDocumentSets();};#endif /* DDOCUMENTSETS_H_ */
cpp文件:
#include "DDocumentSets.h"DDocumentSets* DDocumentSets::_instance;//Jiang DDocumentSets* DDocumentSets::getInstance() {if (_instance == 0) {_instance = new DDocumentSets();}return _instance;}void DDocumentSets::setModel(DDocumentSet* model) {this->model = model;}DDocumentSet* DDocumentSets::getModel() {return model;}DDocumentSets::DDocumentSets() {}DDocumentSets::~DDocumentSets() {}
参考:
http://stackoverflow.com/questions/8612206/linker-error-when-using-static-members
在C++程序中,need to define the static variable in cpp file and not in header file.
在Qt程序中,需要在cpp和header文件中都定义相同名字的static变量。否则编译出错:
undefined reference to xxxx(static 变量)
- 通过singleton模式和global static variable变量做C++程序各个模块之间的数据通信
- Static Variable, Auto Variable, Global Variable, Local Variable, Register Variable (C)
- 静态变量static和全局变量global
- Activity和Fragment之间的数据通信
- Singleton和static的区别
- php 函数变量的作用域 两个关键词global 和static
- static和final修饰的变量之间的区别
- C/C++中static变量和static函数的用法
- C/C++中static变量和static函数的用法
- C/C++中static变量和static函数的用法
- C/C++中static变量和static函数的用法
- PHP变量关键字global/$GLOBAL[]/static
- ARM处理器各个模式之间是如何切换的?ARM各个模式之间切换时,上下文的保存哪些是硬件在做?哪些是操作系统在做?
- VLC各个Module模块之间如何共享变量
- php中的static和global的区别
- ORACLE EBS各个模块之间的关系
- 静态变量 static 全局变量 global
- node.js的global variable,和module.exports
- java观察者模式
- 关于Struts2.hibernate,spring(笔记)
- jQuery学习(三) 事件
- Java中的serialize接口与transient关键字
- linux下存活命令
- 通过singleton模式和global static variable变量做C++程序各个模块之间的数据通信
- HashMap中的内容进行迭代输出
- 重学C++ 对数组进行线性搜索
- android 接入支付宝支付 "订单签名错误,请检查PARTNER对应的密钥是否正确"
- struts2标签的总结
- 用C++实现多线程Mutex锁
- linux内核函数fls()
- Oracle高级函数使用的浅显介绍
- How to Use Custom NSAttributedString Attributes