博客
关于我
Qt C++定义全局变量的两种方式
阅读量:544 次
发布时间:2019-03-09

本文共 565 字,大约阅读时间需要 1 分钟。

使用extern关键字

cglobal.h

#ifndef CGLOBAL_H#define CGLOBAL_Hextern int testValue;#endif // CGLOBAL_H

cglobal.cpp

#include "cglobal.h"int testValue = 1;

调用方式

#include "cglobal.h"qDebug() << testValue; testValue = 2;

使用static关键字

cglobal.h

#ifndef CGLOBAL_H#define CGLOBAL_Hclass CGlobal {public:    CGlobal();    ~CGlobal();public:    static int testValue;};#endif // CGLOBAL_H

cglobal.cpp

#include "cglobal.h"CGlobal::CGlobal() {}CGlobal::~CGlobal() {}int CGlobal::testValue = 1;

调用方式

#include "cglobal.h"qDebug() << CGlobal::testValue; CGlobal::testValue = 2;

建议使用第二种方式

转载地址:http://bbysz.baihongyu.com/

你可能感兴趣的文章
Networkx读取军械调查-ITN综合传输网络?/读取GML文件
查看>>
Net与Flex入门
查看>>
net包之IPConn
查看>>
NFinal学习笔记 02—NFinalBuild
查看>>
NFS共享文件系统搭建
查看>>
nfs复习
查看>>
NFS网络文件系统
查看>>
nft文件传输_利用remoting实现文件传输-.NET教程,远程及网络应用
查看>>
ng 指令的自定义、使用
查看>>
Nginx
查看>>
nginx + etcd 动态负载均衡实践(二)—— 组件安装
查看>>
nginx + etcd 动态负载均衡实践(四)—— 基于confd实现
查看>>
Nginx + Spring Boot 实现负载均衡
查看>>
Nginx + uWSGI + Flask + Vhost
查看>>
Nginx - Header详解
查看>>
Nginx Location配置总结
查看>>
Nginx Lua install
查看>>
Nginx upstream性能优化
查看>>
Nginx 中解决跨域问题
查看>>
Nginx 动静分离与负载均衡的实现
查看>>